News for hackers. Stuff that matters.
Re:Inheritance was invented as a performance hack by bradrobertson on Thursday April 29, 2021 @10:13PM (#26989191)
"Catching on" doesn't mean "is the right way to do things". Code reuse is in fact a prime example of a misuse of inheritance
Re:Inheritance was invented as a performance hack by ahartmetz on Thursday April 29, 2021 @10:36PM (#26989325)
Correctly using a language feature often means using it for more than one reason. Extending superclass code can be well advised in case it also makes sense semantically. Now, I have seen a "mixin" class that just served to provide one relatively independent method, and I turned it into a free function. Much better. In other cases, an extended class works on the whole state of the object. In that case it usually makes sense.
Re:Inheritance was invented as a performance hack by pansa2 on Thursday April 29, 2021 @10:46PM (#26989390)
> Code reuse is in fact a prime example of a misuse of inheritance

Does that depend on how inheritance works in a particular language? For example, in Python it’s widely believed that inheritance is simply “a tool for code reuse” [0].

[0] https://youtu.be/EiOglTERPEo?t=190

Re:Inheritance was invented as a performance hack by hota_mazi on Friday April 30, 2021 @12:03AM (#26989729)
It's the main reason why inheritance became so popular and so useful.

Specialization remains a very common design pattern that is incredibly useful and trivially and intuitively solved with inheritance. No other programming concept (HKT, ad hoc polymorphism, functional programming, etc...) comes close to its elegance.

Re:Inheritance was invented as a performance hack by rectang on Friday April 30, 2021 @02:05AM (#26990271)
> Specialization remains a very common design pattern that is incredibly useful and trivially and intuitively solved with inheritance.

Beautifully put. That matches up with my own experience learning classical OOP, even if I'm now comfortable with other models.

Re:Inheritance was invented as a performance hack by ratww on Friday April 30, 2021 @05:54AM (#26991525)
Code-reuse via "Implementation Inheritance" is completely unnecessary for specialisation or polymorphism.

When someone says that "inheritance is bad for code reuse" they're not talking about interfaces, or using inheritance for polymorphism. They're strictly talking about sharing code using implementation inheritance, which is the thing that has been widely criticised for more than 30 years now.

One can argue that even the "Template Method Pattern" doesn't fall into "implementation inheritance", since the implementation lives in the subclass.

If you read the posts, discussion is way more nuanced than "inheritance bad vs inheritance good".

Re:Inheritance was invented as a performance hack by hota_mazi on Friday April 30, 2021 @11:57AM (#26995136)
Reusing implementation is the point.

Here is more specifically what I meant with my comment about specialization above. There is a class with four methods, three of which are exactly what you need but the fourth one, you need to modify.

Solving this with inheritance is trivial (extend and override).

Solving this with any other paradigm is... much harder and requires a lot more boilerplate.

Re:Inheritance was invented as a performance hack by aflag on Friday April 30, 2021 @01:47PM (#26996800)
In functional programming you'd just create a new function that takes a value of the same type of those other 3 methods.

But anyway, creating a new method without changing any of the methods of the super class I think it's generally ok. The problems arise from modifying methods that the super class already implemented.

Re:Inheritance was invented as a performance hack by hota_mazi on Friday April 30, 2021 @05:03PM (#26999300)
But code that uses the old function wouldn't magically start invoking your new function instead, something that inheritance and polymorphism give you for free.
Re:Inheritance was invented as a performance hack by aflag on Friday April 30, 2021 @06:18PM (#27000176)
Nothing will magically start calling your new function. If you are defining a new function that didn't exist before, then you'll have to actively call it somewhere. What you're describing instead is overriding an inherited function. However, that is full of pitfalls, I would not call that "for free" by any means. There are example of the problems in this very thread. Anyway, that's distinct from polymorphsim, which is present in functional programming.

You may be missing the point that's being made, though. No one is arguing against interfaces, but overriding concrete methods from a concrete class. Those need to be well thought out as extension points for you to have any chance of having stable software. Not quite for free.

Re:Inheritance was invented as a performance hack by hota_mazi on Saturday May 01, 2021 @02:46AM (#27003901)
But that's what polymorphism does.

Somewhere deep in the code is calling a.foo(), but when you pass a subclass of A that overrides foo(), then this code "magically" calls that new implementation.

This is where specialization shines and no other paradigm allows this so elegantly and so simply.

Re:Inheritance was invented as a performance hack by aflag on Sunday May 02, 2021 @01:35PM (#27017093)
That is not exclusive to OO and it wasn't invented by OO either. However, the common pitfall of changing the implementation of a method that was not designed to be changed I would argue to be more common in OO than in other paradigms. That's a common pitfall, though. Not a place where OO shines. Though, to be fair, that's a problem in Java, Python and other popular OO languages, but not inherently a problem of the paradigm per se. C++, for instance, avoids that common issue by not making methods virtual by default.
Re:Inheritance was invented as a performance hack by threatofrain on Thursday April 29, 2021 @10:40PM (#26989350)
Why is code reuse your prime example for bad inheritance? What should people do instead?
Re:Inheritance was invented as a performance hack by rectang on Thursday April 29, 2021 @10:51PM (#26989416)
Interfaces with default implementations are better than classes for a number of reasons, including the diamond problem, and that as systems grow large they almost never fit cleanly into an inheritance tree. Fat pointers with two words, one for an interface vtable and one for the object, work really well.
Re:Inheritance was invented as a performance hack by AnimalMuppet on Thursday April 29, 2021 @10:59PM (#26989457)
In your view, how is "interfaces with default implementations" different from "inheritance for code reuse"? At a minimum it looks like a virtual function with a default implementation in the base class. If a derived class doesn't override it, isn't that code reuse?
Re:Inheritance was invented as a performance hack by rectang on Friday April 30, 2021 @12:09AM (#26989746)
> In your view, how is "interfaces with default implementations" different from "inheritance for code reuse"?

The latter is a more general classification?

A type which inherits a default method implementation from an interface is an example of "inheritance for code reuse".

A type which inherits a method implementation from a parent class under classical OOP is also an example of "inheritance for code reuse".

However, I argue that "interface inheritance with default implementations" is superior to "classical OOP" because it avoids tight coupling with memory layout, problems with implementing multiple inheritance in classical OOP, etc.

Re:Inheritance was invented as a performance hack by loopz on Thursday April 29, 2021 @11:24PM (#26989566)
Maybe he meant something similar to Go interfaces, but they are abstract and coupled loosely with implementations after the fact.

That or abstract class/pure interface. There's no need for default implementations introducing assumptions in code.

Re:Inheritance was invented as a performance hack by rectang on Thursday April 29, 2021 @11:55PM (#26989695)
I believe that both Go and Rust use two-word fat pointers for interface types. I was actually thinking of Rust traits first and foremost; in Rust, traits can supply default method implementations which invoke other methods defined in the same trait.

While I take your point about introducing assumptions, I'm reluctant to give up the convenience of default implementations; they seem to present fewer problems than classically inherited methods because shallow hierarchies are more common with interfaces than with classical inheritance, and because default interface methods cannot directly access member variables because they do not know about object layout — unlike methods inherited from a parent class under classical inheritance.

Re:Inheritance was invented as a performance hack by ratww on Friday April 30, 2021 @05:59AM (#26991562)
"Interfaces with default implementations" are a thing in Java and in some other languages, but most people don't know about them:

https://docs.oracle.com/javase/tutorial/java/IandI/defaultme...

Re:Inheritance was invented as a performance hack by rectang on Friday April 30, 2021 @08:29AM (#26992541)
I knew that Java added "default methods" to its interfaces a few years ago, but wasn't sure whether Java uses "fat pointers" like Rust and Go.
Re:Inheritance was invented as a performance hack by Spivak on Thursday April 29, 2021 @11:11PM (#26989512)
Because interfaces don’t confer a relationship between implementations, only that they have some common method signatures.
Re:Inheritance was invented as a performance hack by username90 on Friday April 30, 2021 @12:37AM (#26989860)
> Because interfaces don’t confer a relationship between implementations

Having the interface implement functions does exactly that, so your point only applies for interfaces without implementations.

Re:Inheritance was invented as a performance hack by jcelerier on Friday April 30, 2021 @04:50AM (#26991157)
> Interfaces with default implementations are better than classes for a number of reasons

it's literally the same thing in practice

> including the diamond problem,

the diamond problem has only ever been a "problem" in OOP textbooks, in years and years of working on OO system I have never saw it be a problem in practice

Re:Inheritance was invented as a performance hack by rectang on Friday April 30, 2021 @08:23AM (#26992486)
Do you know if the mechanism in described by Stroustrup in 1989 for supporting multiple inheritance is actually used by modern C++ compilers?

https://www.usenix.org/legacy/publications/compsystems/1989/...

The plan seems so complex that it makes complete sense to me why languages would avoid multiple class inheritance (where each class is afforded direct access to member variables).

In contrast:

• With single inheritance, you don't have to resolve different member variable layouts because there is only one.

• With interface inheritance (with or without default methods), interface methods don't get to access struct members directly and know nothing about object memory layout.

Re:Inheritance was invented as a performance hack by CRConrad on Monday May 03, 2021 @04:23AM (#27022755)
> Interfaces with default implementations are better than classes for a number of reasons, including the diamond problem [...]

"The diamond problem" only occurs with multiple inheritance. At a (wild-ass) guess, only a minority of "traditional" (=inheritance-based) OOP languages have that; certainly not all of them. So as far as "the diamond problem" is concerned, interfaces with default implementations are no better than single-inheritance classes.

Re:Inheritance was invented as a performance hack by sabellito on Thursday April 29, 2021 @10:47PM (#26989396)
In OO languages, inheritance gets the most bang for the buck when used for polymorphism.

All code reuse can be expressed with composition and delegation, bringing more flexibility, testability, cohesion, decoupling, etc, etc, etc.

Re:Inheritance was invented as a performance hack by loopz on Thursday April 29, 2021 @10:57PM (#26989447)
Inheritance is just a tool to mostly avoid. It tightly couples different class implementations. So will make later refactors error-prone and tend to scatter logic. It's misunderstanding OO as messaging between objects.

Using composition or lay out data differently may yield designs with more desirable properties. There's no one answer and it depends.

Re:Inheritance was invented as a performance hack by Varriount on Thursday April 29, 2021 @10:31PM (#26989292)
How is it a misuse? And what are the alternatives?

To be clear, I do agree with your assertion, however I'm having a hard time actually putting "how/why" into words.

Re:Inheritance was invented as a performance hack by Spivak on Thursday April 29, 2021 @11:06PM (#26989490)
It’s a misuse because (if you really are only using it for code reuse) it’s creating an essential relationship between different objects that is actually incidental.

A list containing two different subclasses of your avoiding-copy-paste base class is now most likely a logical error.

Re:Inheritance was invented as a performance hack by Dylan16807 on Friday April 30, 2021 @12:14AM (#26989768)
> A list containing two different subclasses of your avoiding-copy-paste base class is now most likely a logical error.

So don't allow those? Supporting inheritance doesn't mean you have to support heterogeneous lists.

Re:Inheritance was invented as a performance hack by username90 on Friday April 30, 2021 @12:23AM (#26989803)
You could just as well argue that there should never be any code reuse ever, code reuse always couples objects, code reuse should never be done if the two different parts doesn't have the same purpose. So the same rules for all code reuse also applies for inheritance, if you follow them then there are no problems using inheritance. If you have a set of data kinds which all needs to implement the same fields that has the same purposes then having a list of that parent class doesn't lead to bugs.
Re:Inheritance was invented as a performance hack by throwaway894345 on Friday April 30, 2021 @12:27AM (#26989825)
I don’t think it’s true that reuse means tight coupling. With composition you can swap out the composed object at runtime and as far as compile time coupling is concerned you can embed an interface to decouple the outer object from the inner object.
Re:Inheritance was invented as a performance hack by username90 on Friday April 30, 2021 @12:32AM (#26989843)
All code reuse creates coupling on some level. Creating more abstractions in between reduces coupling but creates code bloat. There is a trade off.

For example, if you call the same function from many locations those locations are now coupled since if you change the function you change all of those locations behaviour. Many times that is desirable, in which case it is good coupling. The exact same rule applies to inheritance.

Re:Inheritance was invented as a performance hack by throwaway894345 on Friday April 30, 2021 @09:20AM (#26993046)
Can you elaborate a bit? How is there coupling between a thing which uses an interface and a thing which implements the interface of the two don’t know about each other? Especially if the interface is implicit à la Go interfaces or structural subtyping?
Re:Inheritance was invented as a performance hack by username90 on Friday April 30, 2021 @04:19PM (#26998760)
Every class implementing the interface needs to follow the same interface contract, that is coupling. If you want to change the interface contract for one of them you have to go and change it for all of them or you have buggy interfaces. Every time you create an interface you create a contract for it, if you have the same discipline with inheritance then inheritance isn't an issue.

The only problem with inheritance is that people can use it for classes that weren't written to be base classes, and therefore a ton of bad programmers use it for code reuse without thinking about the contract it is supposed to implement at all. With that in mind, allowing inheritance only for abstract classes isn't an issue at all.

Re:Inheritance was invented as a performance hack by findjashua on Thursday April 29, 2021 @11:23PM (#26989564)
how is that different from a list containing 2 implementations of an interface?
Re:Inheritance was invented as a performance hack by foo_barrio on Thursday April 29, 2021 @11:46PM (#26989662)
There is no state to track. If there is, 99% composition has worked out way better for me in the long run since I don't have this really strong coupling between two objects.
Re:Inheritance was invented as a performance hack by username90 on Friday April 30, 2021 @12:28AM (#26989826)
So you don't have a need for code reuse then since you don't have many types with the same base structure, just say that instead of saying that it is bad.
Re:Inheritance was invented as a performance hack by catlifeonmars on Friday April 30, 2021 @01:47AM (#26990166)
There are often more straightforward ways to reuse code than to use inheritance. ~Two~ three advantages of composition for code reuse:

1. Composition is often more explicit than inheritance. Inheritance is overly magic.

2. Composition avoids incidental coupling of methods. With inheritance this is unavoidable.

3. Composition is more difficult to misuse. Both composition and inheritance have their purposes. In the wild, I’ve seen inheritance misapplied much more often than I have seen with composition.

Re:Inheritance was invented as a performance hack by signa11 on Thursday April 29, 2021 @11:55PM (#26989696)
yup, totally agree. imho, composition is the right (only ?) way to code reuse.