News for hackers. Stuff that matters.
Re:Inheritance versus Composition by lolptdr on Thursday May 09, 2019 @12:10AM (#19865307)
With all this angst against OOP, what other alternatives should a programmer consider or what patterns of hierarchical logic do people suggest?
Re:Inheritance versus Composition by barrkel on Thursday May 09, 2019 @05:53AM (#19866749)
Hierarchy - a single ontology - is one of the problems with OO. Ontologies - note the plural - are useful, but choosing just one ontology privileges some axis of abstraction over others, but there are different lenses you can put on things that may make you want to classify by different criteria.

For example, you might have a bunch of things, some of which: (a) can be rendered in some display - could be console, HTML, print output, screen, don't care; (b) can be serialized to a variety of media; (c) have some common configuration, that interacts with some configuration management system; etc.

If you try and shoe-horn these commonalities into a hierarchy, you end up with classes that advertise being able to do too much and need to have "not implemented" error conditions, breaking the Liskov substitution principle.

Interfaces are a way of breaking out. They're additive, rather than hierarchical, from the implementing class perspective.

The other big problem (a bigger problem IMO) with inheritance is coupling. I don't think there's any closer coupling between two pieces of code in an OOP system than inheritance. Changes in a base class can radically alter behaviour in all descendants. Depending on the implementation of overriding, descendant behaviour can be accidentally hijacked with no compile time or run time warning merely by updating a dependency. The API by which base class and descendant class interact is bidirectional, with the flavours of both library (you call them) and framework (we'll call you), and intermediate internal state when e.g. overridden protected methods are being called is usually sorely undocumented and liable to change.

Re:Inheritance versus Composition by marcosdumay on Thursday May 09, 2019 @01:16PM (#19870130)
> Interfaces are a way of breaking out. They're additive, rather than hierarchical, from the implementing class perspective.

Java (and .Net by copying) choose to make interfaces additive and superclasses combinatorial. It's a design decision and in no way intrinsic. Other languages have different choices.

Re:Inheritance versus Composition by barrkel on Thursday May 09, 2019 @04:56PM (#19872340)
Oh, I'm aware. However additive superclasses are much much worse for OOP, there's good reason languages beyond C++ haven't walked the same road.
Re:Inheritance versus Composition by weberc2 on Thursday May 09, 2019 @01:26AM (#19865637)
Use composition for reuse and interfaces for polymorphism. These can do the same as inheritance and more, and they do it more cleanly. Reuse and polymorphism are orthogonal concerns that inheritance tries to fuse awkwardly together.

Try Go if you’d like a familiar language without the inheritance that you can pick up in an afternoon.

Re:Inheritance versus Composition by tonyedgecombe on Thursday May 09, 2019 @05:19AM (#19866618)
It's not compulsory even in a language that supports it, I'm working on a medium sized C# project that has minimal use of inheritance and only then to fit in with the requirements of a library feature.
Re:Inheritance versus Composition by weberc2 on Thursday May 09, 2019 @08:59AM (#19867850)
Yeah, I meant it as a learning suggestion, not a “switch your main language” suggestion. Fully agree with you.
Re:Inheritance versus Composition by cuddlecake on Thursday May 09, 2019 @01:01AM (#19865536)
The first question should be whether we need hierarchical logic, or what hierarchical logic is supposed to be.
Re:Inheritance versus Composition by userbinator on Thursday May 09, 2019 @08:01AM (#19867407)
It's even harder to define (which might be why OOP is so popular --- it's easy to get dogmatic about "OOP is X", and the cargo-culters will pick it up and spread it like religion), but I'd say "be pragmatic and let it come naturally". Systems tend to naturally form OO-ish things even if they're not written in an OO language; the Linux kernel is one example. I think of OO as just another layer of organisation beyond functions and structures, arising from the desire to group related data and code together.
Re:Inheritance versus Composition by zzzcpan on Thursday May 09, 2019 @09:07AM (#19867899)
It's not natural. It depends on your previous experience. Preferring C-style semi-OO stateful programming and OO merely shows what influenced your thinking.
Re:Inheritance versus Composition by tabtab on Thursday May 09, 2019 @11:07AM (#19868941)
In my opinion we need look at bringing the relational model into code, not just data. OOP is too awkward for complex relationships. Or, at least making code more relational-friendly to avoid the O-R-Impedance-Mismatch: https://en.wikipedia.org/wiki/Object-relational_impedance_mi...
Re:Inheritance versus Composition by nradov on Thursday May 09, 2019 @11:22AM (#19869080)
There's nothing particularly wrong with OO as a programming paradigm when used for the right purposes. It was originally designed for simulating real world systems and still works great for that.
Re:Inheritance versus Composition by collyw on Thursday May 09, 2019 @04:16AM (#19866323)
Aren't most large systems organized in an OOP manner?