Why general inheritance is flawed and how to finally fix it
41–50 of 104 posts
Re: Why general inheritance is flawed and how to finally fix it
#42I 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…
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
#43https://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.
Re: Why general inheritance is flawed and how to finally fix it
#44Earlier 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…
Re: Why general inheritance is flawed and how to finally fix it
#45There are also problems with the combination of inheritance and concurrency. Google for "inheritance anomaly".
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
#46Inheritance 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.
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
#47The 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…
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
#48Earlier 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.
(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
#49Re: 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…
Re: Why general inheritance is flawed and how to finally fix it
#50Fantastic 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…
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.