Live data from Hacker News

Why general inheritance is flawed and how to finally fix it

minborgsjavapot.blogspot.com

41–50 of 104 posts

Re: Why general inheritance is flawed and how to finally fix it

#42

I think that composition is absolutely better than inheritance except for one thing: boilerplate. The issue is that boilerplate is kind of important. You don't want to litter your code with "f150.ford.car.vehicle.object.move(50, 50)". You can and should re-implement "move" so that you only have to call "f150.move(50, 50)", but that still requires boilerplate, just in the "F150" class. Often you have class containing…

COM solves this with delegation, where objects can only implement the methods that they care about and delegate everything else to the aggregated type, which provided the full interface.

However, depending on which stack one is using (VB 6, .NET, MFC, ATL, WRL, WinRT), the amount of boilerplate to deal with the runtime differs.

Re: Why general inheritance is flawed and how to finally fix it

#43
post #40
post #34

https://lwn.net/Articles/548560/ I really enjoyed the article above, which I read many years ago (before Rust 1.0!) which discusses how Golang and Rust handle polymorphism and code-reuse without classic object inheritance. My current thinking is that software objects are a general-purpose tool, but classic object inheritance should rarely be used as it is a solution to a narrow problem—classes should be "final" by de…

I mostly agree, but there's one place where it does make a lot of sense to keep the hierarchy open: exceptions. Ability raise a specific error and catch it in a generic handler is very useful.

Interfaces would work fine here as well.

Re: Why general inheritance is flawed and how to finally fix it

#44

Earlier quoted context omitted.

Language Design: Let's start with the admission that there's a lot of cargo culting in language design. Evidence is hard to come by and the best evidence is other languages that succeed with different choices. I remember the flame wars about how multiple inheritance would cause a language to fall apart. When will languages adopt the idea that encapsulation is not sacrosant, as the theoretical issue about the backdoor…

> Language Design: Let's start with the admission that there's a lot of cargo culting in language design. Evidence is hard to come by and the best evidence is other languages that succeed with different choices. You are going to get a lot of false positives using success as a metric for good language design. I cannot think of a single popular language, other than Python, in the last 40 years that did not have huge co…

People keep telling this about Python, always ignoring who was paying Guido's salary.

Re: Why general inheritance is flawed and how to finally fix it

#45

There are also problems with the combination of inheritance and concurrency. Google for "inheritance anomaly".

Thank you, interesting read, haven’t heard of it before.

Though I feel it would be dishonest to “blame it” on inheritance over on concurrency itself, when we don’t have any good solution to general concurrency as far as I know. We can only deal with it reasonably well by heavily restricting the domain-space to begin with (eg. immutables, no globals/sharing).

Re: Why general inheritance is flawed and how to finally fix it

#46
post #36

Inheritance is flawed, in Java, mainly because it is the only organizing principle offered, so gets shoehorned into all kinds of problems where it is a poor fit. Inheritance is just the right thing once in a while, but Java coders are obliged to apply it well beyond its useful range.

Just because it exists doesn’t mean it has to be used beyond its intended domain. One is entirely free to create flat “hierarchies” in Java. But I agree that in hindsight, final classes as a default would be better.

Fortunately nowadays, records and sealed classes remedy this for the most part in java.

Re: Why general inheritance is flawed and how to finally fix it

#47
post #31

The author is onto something but I’m afraid it’s not explained very well. I think he’s mostly right though. While reading it I was reminded of a design/implementation style I’ve run across several times over the years which is to find an existing class that does something similar to what you want. Then, subclass it and override methods until you get the behavior you want. And you’re done! This leads directly to the F…

I have to agree with you. On the other hand, closed hierarchies can be an elegant solution to certain problems. Eg. sealed classes (and basically their single-class counterparts, final classes) avoid the mentioned problems in Java’s parlance.

One exception comes to mind though — Java’s SAMs, or in the general case, classes that more or less only wrap around a few methods intended to be overridden/implemented with clear requirements (but maybe this use case also should be restricted to interfaces?) But the default should be to add an explicit open instead of defaulting to non-final.

Re: Why general inheritance is flawed and how to finally fix it

#48
post #29

Earlier quoted context omitted.

I mean, I'm not sure where this 'fear' even came from? At its core, inheritence is a special case of composition anyways (looked at from the other perspective it's syntactic sugar over either static or dynamic delegation), so it can't really be "faster". At any rate, there's no abstraction so powerful it can prevent a programmer from making it slow.

This is true for languages where most objects are not garbage-collected. In Java (modern days) this would add a level of indirection.

Which may be in certain cases trivially optimizable. Eg. an object available only inside a class can have its method calls inlined into the containing class’s methods.

(But I’m no JVM developer (unfortunately) so I’m not sure whether this exact optimization exists or not)

Re: Why general inheritance is flawed and how to finally fix it

#49
post #3

Re: using composition In the past, it was feared that this would lead to reduced performance but this is simply not the case. Great to see the strong evidence here /s

Language Design: Let's start with the admission that there's a lot of cargo culting in language design. Evidence is hard to come by and the best evidence is other languages that succeed with different choices. I remember the flame wars about how multiple inheritance would cause a language to fall apart. When will languages adopt the idea that encapsulation is not sacrosant, as the theoretical issue about the backdoor…

Spring does (and perhaps even prefers where possible) constructor-based inheritance though — which is as pluggable as it gets, making testing easy.

Re: Why general inheritance is flawed and how to finally fix it

#50
post #39

Fantastic article! Object oriented programming gets a horrible wrap on the basis of inheritance alone, and it's no wonder. Outside of limited domains, such as GUI programming, object inheritance makes little sense. Computer science students are right to question their introductory classes on inheritance when they teach contrived examples of dogs barking and cats meowing as an example of Mammal.makeSound() inheritance…

> Duck typing or traits are better ways to represent polymorphic behaviors. We've known this for over a decade now.

Any objective measure of that? Because there is a catch, we can’t do what doctors can. There are no double-blind tests for language design. All we have is empirical studies and based on that, OOP languages do objectively much better. So if anything, your exceptional claim require exceptional evidence.

But otherwise I agree that these Animal hierarchies are just dumb and many definitely overuse inheritance.

Post reply on HN