News for hackers. Stuff that matters.
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.