Live data from Hacker News

If Inheritance is so bad, why does everyone use it?

buttondown.email

311–320 of 389 posts

Re: If Inheritance is so bad, why does everyone use it?

#311

Earlier quoted context omitted.

Java complexity is also an artifact of Java being used in large complex systems. People are really complaining about how hard programming is. There are many types of large scale systems where I would only code it using the JVM, everything else is a nightmare. Big tech companies largely feel the same

> There are many types of large scale systems where I would only code it using the JVM, everything else is a nightmare. I’ve seen more than one lifetime’s worth of nightmare code written in Java. I agree with the GP commenter - I don’t think the problem is Java (the language) so much as the culture surrounding it. The Java programmers I’ve worked with (all smart people) seem addicted to solving all problems my writin…

> I’ve never seen this abstraction vomit disease be quite this bad in software written in other languages. You can find a bit of it in C#, C++, go and Python. But not as bad as Java

That’s just your personal experience with these languages. I would even go as far to say that there is some survivorship bias — certain projects might have survived in java that collapsed under its own weight in some other platform, making the “complex nightmare projects” overrepresented in java, even though it is due to a positive thing.

Re: If Inheritance is so bad, why does everyone use it?

#312

The key is "prefer composition to inheritance" and dates back to Gang of Four. The word "prefer" is critical to understand. It just means "usually choose A over B" not "B is never the right answer." Unfortunately, since we - as an industry - like hard and fast rules, we move towards that second explanation and act like inheritance never makes sense. Like any tool, there's a time and place where it is the best tool, o…

Yes and:

I vaguely recall Riel's Object-Oriented Design Heuristics [1996] shared the same advice. https://www.amazon.com/Object-Oriented-Design-Heuristics-Art... https://www.oreilly.com/library/view/object-oriented-design-...

Aside: Young me obsessed over design patterns, methodologies, software engineering, SQA, etc. I had all the books. I started a design patterns study group and hosted it for a while. Now all that "wisdom" is just a bunch of old books. Somehow craftsmanship, me, or both became irrelevant.

Or maybe it was never important.

Like Alistair Cockburn opined about the failure of CASE tools, people somehow manage to ship software successful, without the benefit of all that smart stuff.

Re: If Inheritance is so bad, why does everyone use it?

#313

The key is "prefer composition to inheritance" and dates back to Gang of Four. The word "prefer" is critical to understand. It just means "usually choose A over B" not "B is never the right answer." Unfortunately, since we - as an industry - like hard and fast rules, we move towards that second explanation and act like inheritance never makes sense. Like any tool, there's a time and place where it is the best tool, o…

I'd be ready to agree if I could be pointed at a time that inheritance actually carries a real benefit- a time you would choose it over composition, if composition is available.

There is no benefit to the "tree of life" single inheritance [1].

What you want to reach for to achieve compositional behavior or polymorphism are type classes, traits, and interfaces. They attach methods to data without the silly "Cat is an Animal, Dog is an Animal" cladistic design buffoonery.

There's nothing wrong with OO, except for inheritance-based OO.

[1] Don't get me started on multiple inheritance. Instead of solving problems, they invented them.

Re: If Inheritance is so bad, why does everyone use it?

#314

Inheritance is really just a public interface, a private interface and automatic delegation of those interfaces to the base class. The problems with inheritance are: - people shove code in the base class to dedup it without thinking about design - people add public and protected methods without thinking about interface design - the names of the base class and the public and protected interfaces are exactly the same t…

It's worth mentioning regarding the IFoo example that in a single project you can always just find/replace FooBase with IFoo or even FooBaseV2.

I feel like a lot of people blow this up like it's so e huge problem when in reality this is a pretty trivial refactor.

Re: If Inheritance is so bad, why does everyone use it?

#315

The key is "prefer composition to inheritance" and dates back to Gang of Four. The word "prefer" is critical to understand. It just means "usually choose A over B" not "B is never the right answer." Unfortunately, since we - as an industry - like hard and fast rules, we move towards that second explanation and act like inheritance never makes sense. Like any tool, there's a time and place where it is the best tool, o…

Would you happen to know any good literature with clear composition examples vs inheritance?

The C++ and Python code I see daily is fully inheritance focused, and I would like to understand how it could be done differently (or better) starting from a perspective I understand.

Re: If Inheritance is so bad, why does everyone use it?

#316
post #71

Earlier quoted context omitted.

Polymorphism is doable in plain old C with lookup tables and function pointers. If that is the only benefit, what is the point of creating a language where everything is an object?

> Polymorphism is doable in plain old C with lookup tables and function pointers Not without casting. qsort is still: void qsort_r(void *base, size_t nmemb, size_t size, int (*compar)(const void *, const void *, void *), void *arg);

Presumably one would write an object-oriented version of quicksort to go with their OO C library design and not use the stdlib functions. For example GObject is OOP in C, and I imagine it has collection types with sort methods.

Inheritance is what will bite hard when hand-rolling OOP in C. For one, you can forget about the compiler enforcing substitutability and co/contravariance for you.

Re: If Inheritance is so bad, why does everyone use it?

#317
There is no valid use case I've ever seen where inheritance was better than composition (that does not mean a use case does not exist, but evidence is mounting against it). I would say it's used because every language made a poor decision by building that in as the way to encapsulate reusable logic. i.e., a mistake.

Some APIs may expose themselves as requiring you extend a base class, and in that case you might as well do what the library author said. But overall? Yes, some things can work well with inheritance (eg. classical UI frameworks), but these are often equally good with a composition based API.

One job I was at I added a lint rule to flag `extends` as an error. You would need to add a comment to justify why you were using inheritance - it went over more favourably than I expected. I think I would suggest this for any new projects using a language that supports inheritance - discourage it with automation, but allow really good reasoning to prevail in review. It is the worse solution at least 99% of the time because it requires you predict a future that is painful to change.

Re: If Inheritance is so bad, why does everyone use it?

#318
post #59

My impression is that inheritance is in many common programming languages the easiest way to share code. Sharing code in another way would require some more thoughts and sometimes code, so the lazy programmer takes inheritance. Traits as a general concept would be very useful in many programming languages, but only some (like Scala) have proper support for it. In principle a Java interface with default methods (imple…

Rust also has traits, and no struct inheritance. I think it works quite well.

Re: If Inheritance is so bad, why does everyone use it?

#319

Earlier quoted context omitted.

I'd be ready to agree if I could be pointed at a time that inheritance actually carries a real benefit- a time you would choose it over composition, if composition is available.

There is no benefit to the "tree of life" single inheritance [1]. What you want to reach for to achieve compositional behavior or polymorphism are type classes, traits, and interfaces. They attach methods to data without the silly "Cat is an Animal, Dog is an Animal" cladistic design buffoonery. There's nothing wrong with OO, except for inheritance-based OO. [1] Don't get me started on multiple inheritance. Instead o…

Yes. I hadn't heard the term tree of life inheritance, but that is the heart of the problem. I think this could be somewhat mitigated by banning access to non-abstract parent methods and all grandparent methods (as well as all relations that aren't directly reachable by going up the tree). But at that point you might as well just use composition anyway.

Re: If Inheritance is so bad, why does everyone use it?

#320
post #142

Earlier quoted context omitted.

>For me the main point is (runtime) polymorphism. But you don't actually care about runtime polymorphism here. You care about polymorphic behavior, which can be implemented in a much more composable way with parametric polymorphism.

You can’t build a dynamic list of objects implementing the same interface in different ways with parametric polymorphism. As another example, the Unix file interface ( open() , read() , write() , flush() , close() ) etc. is an example of runtime polymorphism, where the file descriptors identify objects with different implementations (depending on whether it’s a regular file, a directory, a pipe, a symbolic link, a de…

So you think a List[Function[Unit]] all does the same thing?
Post reply on HN