Tag Info

Hot answers tagged

29

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 ...


26

Is spatial efficiency (and improved spatial locality, especially in large arrays) the only reason why fundamental types are often not classes? No. The other issue is that fundamental types tend to be used by fundamental operations. The compiler needs to know that int + int isn't going to be compiled to a function call, but to some elementary CPU ...


18

Yes, it pretty much comes down to efficiency. But you seem to be underestimating the impact (or overestimating how well various optimizations work). First, it's not just "spatial overhead". Making primitives boxed/heap-allocated has performance costs too. There's the additional pressure on the GC to allocate and collect those objects. This goes doubly if ...


14

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

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 ...


8

It depends on the language you are using (and sometimes on how the methods are declared). There are essentially two ways for the implementation to perform method calls: static dispatch and dynamic dispatch. In static dispatch, the compiler determines at compile time exactly which method to call, without taking the possibility into account that there might ...


8

Simple objects are just a struct containing all the fields. Methods typically take a pointer to that struct as their first argument (like python does). This simple form of OOP is done all the time in C. Inheritance makes things more complicated. You can add a pointer to the parent object from the child object, or include all the parent's fields. Virtual ...


8

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 ...


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 ...


5

It's not entirely clear whether you are talking about the semantics or the pragmatics here. Your question reads more about semantics, but in the comments you say you are asking about pragmatics. I'm going to answer about both. Semantics Pros: simplicity. Why have two concepts when one will do? Cons: none. Pragmatics Pros: simplicity. Again. Cons: ...


5

Remove the duplicate test code Having tests on your child-classes for code that is in the parent class means twice the maintenance if the parent class changes. The reason you move to a parent class is specifically to avoid duplicating code, actual code as well as tests. (And if you ever end up creating a third child you might end up thinking that all tests ...


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.


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 ...


4

Even when the method call originates in the superclass, it should call the overriding method in the subclass. This behaviour is sometimes called “open recursion”, and this feature is crucial for techniques like the “strategy pattern”. Open recursion is generally considered to be a fundamental concept of object-oriented programming, although it is not without ...


4

There are only very few cases where you need “fundamental types” to be full objects (here, an object is data that either contains a pointer to a dispatch mechanism or is tagged with a type that can be used by a dispatch mechanism): You want user-defined types to be able to inherit from fundamental types. This is usually not wanted as it introduces ...


4

If the interfaces are empty and you find yourself querying: if (someObject is IClickable) { ... } then those interfaces are probably not very useful and can be replaced by boolean values. If on the other hand the interfaces contain members which are actively used, for example: clickableObject = someObject as IClickable; if (clickableObject != null) ...


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 ...


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 ...


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 ...


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 ...


4

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 ...


4

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 ...


3

As far as I know, it's simply a question of C++ having this syntax (and it needed some syntax because the language was not strong enough to support implementing new T(); at that time as a library) and then inherited. From memory, Java inherited it from C++, JavaScript picked it up from Java, and so did C#. Since then, it seems to have become pretty standard. ...


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 ...


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 ...


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 ...


3

Generic functions should not go into classes. Python is not Java, it does not require pure functions to live inside classes. Only use classes if you want to store state of some kind. Put the functions into a standalone utility module, and import that into the module that contains your test classes.


3

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 ...


2

Answering in a very abstract and technology/framework agnostic manner, my thinking is as follows What a GUI control is (ie. volume up, volume down) not equates to how it is positioned in a window (a frame of visual reference). Therefore separate the functionality of laying-out from the controls themselves. Use a factory to create various layout managers ...



Only top voted, non community-wiki answers of a minimum length are eligible