Tag Info

New answers tagged

0

An abstract class mean that some parts of its implementation may be missing. IMHO, the fact that a Gauge may be useless on its own is your opinion, not the opinion of all the users of your API. I would only declare such a class abstract if it lacked important methods, but never as a mean to prevent the user from instantiating such an object. First, I would ...


5

Let's say you had a software company where you wrote software for any and all things related to cars. Should you have one Car object that is reused in all your software? Of course not: On a sales floor, a Car has retail and wholesale values. To a mechanic, a Car consists of a bunch of replaceable parts. To an engine computer, a Car consists of a bunch of ...


0

From my point of view, yes it is. The only question is, do you want a class, or an interface is enough? So the question will be: do you have common behaviour (method implementation) between child classes, so you need a common abstract class, or just method declaration (in this case, better use an interface)? D.


1

Usually Abstract classes are used when one has a class which has some methods which needs to be implemented by one of its child classes, thus, when one sees an abstract class one also expects to see a series of abstract methods which must be implemented. So although you can use abstract classes to denote non instantiable classes, I do not think that if you ...


2

The first problem with trying to be true to the real world is that we don't understand it completely. There are still plenty of open problems in physics. To solve a problem tied to the real world you must first pick a model of it, and any choice will be incomplete and to a certain extent inaccurate. The second problem is that even if you did understand the ...


2

One reason for introducing a separate variable is to improve readability, assumed the real name of $someClass->thisVar() is not expressive enough. This can make sense even if the function is only called once: function someFunction(SomeClass $someClass) { $explainingName = $someClass->thisVar(); doSomethingElse($explainingName); } Of course, ...


5

It is impossible to answer the question of whether the code you present is a good design without a good understanding of what and who the code is for. Why, exactly, do you have such a class in your application? What are its clients? Who specified the behaviours that are implemented by it? How likely are those specifications to change in future? Which aspects ...


3

To answer the question in the title, it is not acceptable to model real-world items with classes if those real-world items do not play a role in your problem domain. To give an extreme example, it is not good to have a class House when you are modelling a railway network. The criticism in the question you linked to was that the asker stated (paraphrased): ...


1

I don't know if this is what you're looking for but here goes. Methods like save, get, getBy, getAll, etc, are generic and would ideally work across multiple models. For the most part, the only difference is going to be SPECIFICALLY where that data is. So why not make a super model of sorts for all your models to extend. class Super_Model { public ...


1

Karnaugh Maps are a great way to reduce logic complexity. Karnaugh Maps notwithstanding you want to clearly define the various states and their basic transition logic. Regardless of how much you can reduce the number and complexity of logic expressions you must carefully analyze states (for example "UpdatingProfile") and make methods accordingly. The ...


0

My thought is that it is correct and usable, but not necessarily good practice. My understanding of an interface is to promote (among many things) SoC, not to store credentials for a specific database. Intermingling connection information with the interface limits the use and purpose of the interface.


4

FP does indeed strive for a reduction in the representational gap: Something you'll see a lot of in functional languages is the practice of building the language up (using bottom-up design) into an Embedded Domain Specific Language (EDSL). This lets you develop a means of expressing your business concerns in a way that is natural for your domain within the ...


5

Most functional languages are not Object-Oriented. That does not mean they have no objects (in the sense of complex types which have specific functionality associated with them). Haskell, like java, has Lists, Maps, Arrays, all kinds of Trees and many other complex types. If you look at the Haskell List or Map module you will see a set of functions very ...


2

As Niklaus Wirth put it, "Algorithms + Data Structures = Programs". Functional programming is about the way to organize algorithms, and it does not tell a lot about ways to organize data structures. Indeed, there exist FP languages both with mutable (Lisp) and immutable (Haskell, Erlang) variables. If you want to compare and contrast FP with something, you ...


7

I would like to stress an aspect that I find important and that has not been covered in the other answers. First of all, I think that the representational gap between problems and solutions can be more in the mind of the programmer, according to their background and to the concepts they are more familiar with. OOP and FP look at data and operations from ...


18

The basic data is structured the same in pretty much any paradigm. You're going to have a Student, a Course, etc. whether it's an object, a struct, a record, or whatever. The difference with OOP isn't how the data is structured, it's how the functions are structured. I actually find functional programs much more closely match how I think about a problem. ...


9

When I took my Java class years ago, we were expected to show our solutions to the entire class, so I got to see how people think; how they solve problems logically. I fully expected the solutions to cluster around three or four common solutions. Instead, I watched as 30 students solved the problem in 30 completely different ways. Naturally, as fledgling ...


-1

I did a quick microbenchmark regarding this and I have provided full sources in github. My conclusion is that whether creating objects is expensive or not is not the issue, but continuously creating objects with the notion that the GC will take care of things for you will make your application trigger the GC process sooner. GC is a very expensive process ...


6

C# does have immutability built in. So does Java, and VB.NET. Mutable: public class Counter { private int count; public Counter(int initialCount) { count = initialCount; } public void Increment() { count++; } } Immutable: public class Counter { private readonly int count; // `final` for Java public ...


4

Check before, please. Which would you rather read and debug, after the code has been modified a few times. This? function someFunction() { if($condition1) { if($condition2) { if($condition3) { //Execute code } else { throw new Exception("Cant execute code because the third condition is ...


2

If a method uses $this it cannot be called statically. Or at least it shouldn't be called statically even if some sort of weird PHP behavior allows it. You never know with PHP. If you insist on keeping your architecture as is, i.e. with monsters drawing themselves (which is not a good architecture, like Doval said), the simplest way would be something like ...


2

The Liskov Substiution Principle is by far the best "heuristic" I know of for determining whether direct subclassing is a good idea. Say you have a base class Foo, and derived classes Bar1 through Bar9. In a nutshell, the LSP states that any block of code using a Foo must work correctly no matter which of the nine Bar types that Foo actually is. From the ...


0

One common use is when you want to use the template method pattern. I that pattern you write the skeleton of an algorithm in an abstrac class. In some parts of that skeleton you can certain methods that are not implemented. Subclassed implement those methods completing the algorithm. That can also be achieved without inheritance using injection and or ...


1

It depends on the conventions and guidelines for a specific programming language. In C# it is not easy to delegate methods to an inner object (it requires explicit code). So inheritance can be easier. When creating a public .net based API, inheritance is often recommended, to simplify discovery for the API user.


1

Somewhere in your code you have to store the specifics of each product. In your example, the natural place for this seems to be the Specs class, since I guess it is something like the "configuration" of your library. I would extend that class, for example, by an attribute "calcFactor", so your calcVar function will look like this: public function calcVar() ...


0

There is a DI framework for C++ (still under development AFAIK): Boost.DI. There are some useful comments about the framework on reddit.


0

With the constrints you having (unable to use a common interface) for ModelA and for ModelB, there isn't a lot of things you can do from the Models. However you could generalize the helper class. One way to do is, public class RequestDTO { string xyz_blah; } public class ResultDTO. { string ProductCheckField; } public class HelperClass { ...


4

Algebraic data types are the way to discuss this. There are three fundamental ways you can combine types: Product. That's basically what you're thinking of: struct IntXDouble{ int a; double b; } is a product type; its values are all possible combinations (i.e. tuples) of one int and one double. If you consider the number types as sets, then the ...


0

What you have shown of the Models shows the same interface. So your protestations of differences are unsupported by the example you give. I suggest you wrap the fields you check in performX and checkY with accessor methods that are commonly named across model A and be and extract a common interface for the use of your Helper class. public interface ...


5

Types are not sets. You see, set theory has a number of features which simply don't apply to types, and vice-versa. For instance, an object has a single canonical type. It may be an instance of several different types, but only one of those types was used to instantiate it. Set theory has no notion of "canonical" sets. Set theory allows you to create ...


2

A type is a description of a category/range of values, compound structures, or what have you. OOPwise, it is akin to an "interface". (In the language-agnostic sense. The language-specific sense, not so much. In Java, for example, int is a type, but has no relation to an interface. Public/protected field specifications, as well, are not part of an ...


2

Sorry but I don't know about the "raw" theory. I can only provide a practical approach. I hope this is acceptable at programmers.SE; I'm not familiar with the etiquette here. A central theme of OOP is information hiding. What the data members of a class are, exactly, should be of no interest to its clients. A client sends messages to (calls methods / ...


4

It's an abstraction as long as you work with it without knowing how its internal work. For the developer of a class, it's not an abstraction until he works on something different (maybe a client of that class). For an integrator who has to make it work with a related class in the same project, it's sometimes an abstraction (when he only uses its API and not ...


0

Google's C++ Style Guide asks you to refrain from doing work in constructors. Their arguments may or may not be applicable to your language. Testing on the Toilet's six-year-old advice #3 on how to write testable code by not doing work in the constructor still holds though. If you search for it online you'll find tons, /*Programmers*/ is one place to start. ...


30

Neither. I take it you're asking whether having the same set of field types is enough to classify as being the same class, or whether they have to be named identically as well. The answer is: "Not even having the same types and the same names is sufficient!" Structurally equivalent classes are not necessarily type-compatible. For instance, if you have a ...


3

Usually constructors are not used to initialize external resources, there are few reasons, Testability - It would be very hard to create unit tests To be in compliance with SRP You could always pass the message queue to the constructor where you use it. class QueueProcessor { private IMessageQueue _queue; public QueueProcessor(IMessageQueue ...


5

Alan Kay, who coined the phrase object-oriented programming has remarked here and in other places, that object-oriented programming is more about messages than objects. The key is that programs are designed as a set of objects that communicate through messages (in many languages a message is referred to as a polymorphic method call). In Smalltalk (the ...


5

You can design a program without actually writing code.


1

Inheritance is another form of containment even if the inheritance is interface implementation. Without leveraging inheritance, you lose a lot of the strength of object-oriented programming. Inheritance allows you to substitute implementations of a class without impacting the client of the class. Think of a UI framework (like MFC or WPF). A lot of the ...


3

Most OO principles depend on inheritance, abstraction and interfaces. So inheritance and interface implementation plays major role in object oriented software design. You may want to read about SOLID and other object oriented design principles. Of course it is possible to write programs in object oriented languages without using inheritance (and more ...


9

If we don't have inheritance our program is not object oriented? Umm, no. I expect that a majority of programmers would still consider a program with traditional objects (bundles of related state and functions) as objected oriented, even if there is no inheritance. In the past handful of years, it has become widely accepted that inheritance of ...


3

This isn't an answer to your question, but a solution to your problem. The issue here is that $Monster->drawSprite() is a non-starter; having monsters know how to draw themselves is a violation of the Single Responsibility Principle. For that same reason it doesn't make sense to try to make a method pull "double duty" by being both an instance method and ...


0

Identifying the pattern. This pattern is actually pretty smart. In fact it's so useful that Microsoft has written a considerable and well liked library called Reactive Extensions to solve this using a technique called functional reactive programming. The pattern you're trying to name is called an Observable. It generalizes an IEnumerable fundamentally. You ...


1

This is a tricky question :) If I really have to choose from only this two options, I would choose A for the simpler form of your task and B for the extended. That is because events does not really make sense until you are calling exactly one function. The primary responsibility of your nodes is to filter the data that they receive. Still when you change ...


0

In a single application references are easier. Above all, they will allow you to process data asynchronously. If your nodes can be located in different applications (even in different computers) you'll need an event-based pattern like Message Queue. This pattern can be implemented with help of .NET events in each single application. So, if you don't plan ...


4

It depends on the layering of the design. If "nodes" are all in the same layer conceptually, then I would prefer references over events, especially read-only references. References are simpler and easier to trace with static analysis tools: References can be read-only fields, whereas event fields are always mutable. A reference field refers to a single ...


1

The answer is wrapping, but it is considerably more work than implied here. I see why you are trying to do this though, because after a certain size of the code base it indeed can be a nightmare to work with typecasting every time you call a library function. Subclassing CustomList is not advisable, as it would expose all the functions that return or expect ...


0

Just create helper class with static helper methods. Example: public class DataHelper { public static String asString(final Object value) { return asString(value, null); } public static String asString(final Object value, final String defaultValue) { if (value instanceof String) return (String) value; ...


0

It's hard to understand fully what you need done here. It looks like you MIGHT have a case for the Visitor pattern but you said you can't use the same interface. The only other alternative would be the Strategy pattern where the CheckMethod would be implemented in a strategy class for each Model and instead of the overloaded methods you can use generics to ...


2

You seem to have some sort of mental block going on. If you want a design that eliminates the duplication, you're going to have to change your design somehow, which you seem resistant to do. You need to look past your current design. Some ways to eliminate the duplication, some of which are better than others: Put the common stuff of the models into a ...



Top 50 recent answers are included