5 ms·
It’s true. But eventually a junior developer realizes that a Triangle is an PolygonalOpenFrameMetallicPercussionInstrument and it now has 4kb of private state,
by swatcoder 4y ago
It’s true.
But eventually a junior developer realizes that a Triangle is an PolygonalOpenFrameMetallicPercussionInstrument and it now has 4kb of private state, 307 inherited member functions, and floods your console when its logged.
I love OOP, but caution is indeed warranted.
- pier25 4y agoYou can totally have OOP without inheritance. In fact "composition over inheritance" is a thing in the OOP world. https://en.wikipedia.org/wiki/Composition_over_inheritance https://en.wikipedia.org/wiki/Composition_over_inheritance
- WesolyKubeczek 4y agoBut why not have both?
- adgjlsfhk1 4y agoinheritance makes your code slower even if you don't use it.
- WarChortle18 4y agoThat's a very broad statement care to back it up?
- Kim_Bruning 4y agoPresumably thinking of virtual methods, in the context of C++ ? At least that used to make your code very slightly slower. Might be measurable in a tight loop?
- adgjlsfhk1 4y agoThis is one of several places. Some others are. You don't know how much memory an object of a given type takes to instantiate. This makes object allocation slower. This also means that you can't allocate a Vector<T> inline. It has to be a vector of pointers. You don't know what fields a type has. This makes GC scanning more expensive. You can't optimize the layout of a type. If you know an object doesn't have subtypes, you can move it's fields around to reduce padding. If you want to be able to look up fields quickly, you need to put all the fields of the supertype first in memory which will increase the size of your objects.
- pjmlp 4y agoYou can surely allocate a Vector<T> inline, no need for pointers, just make use of compile time polymorphism instead of runtime one. On OOP languages with GC, you can reduce GC scanning by having precise GC, and if the languages support value types by making use of structs with method pointers. With the help of PGO for AOT or the JIT, the compiler can optimize the layout of types for the target architecture.
- kaba0 4y agoYou are comparing having a feature vs not having it. To make it fair you have to provide for polymorphism, and at that point most of your points become moot in lower level languages as well. Also, bounded subclassing exists (sealed/final classes) so in theory most/some of the optimizations you mention are possible.
- adgjlsfhk1 4y agoIMO, the best way to do this is to provide multimethods to give you both polymorphism while letting the compiler know how objects are laid out.
- pier25 4y agoOf course but a lot of people associate OOP with inheritance and there are languages like Go that don't even have inheritance.
- alexalx666 4y agoin fact I dont remember when I last used inheritance, protocols and composition used a lot
- tyurok 4y agoIt's a thing because inheritance got out of hand. Inheritance x composition is orthogonal to OO.
- erokar 4y agoSure you can — in your own code. But everywhere else inheritance is a virus. Spring, Rails, .NET Core, Django, Pydantic — just to take some random examples are all fundamentally base on inheritance.
- pyrale 4y agoAh yes, "You wanted a banana but what you got was a gorilla holding the banana and the entire jungle".
- pipeline_peak 4y agoWhat is a private state and how would you know its size?
- zozbot234 4y agoNot a concern if PolygonalOpenFrameMetallicPercussionInstrument is just a pure interface/type-class/trait. Now, is that (i.e. interface-only inheritance) still OOP proper, or just a case of "object based" coding, a straightforward "common sense extension" of simple data abstraction? People will disagree about this point, and it's mostly just a matter of definition.