Live data from Hacker News

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

buttondown.email

151–160 of 389 posts

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

#151

Earlier quoted context omitted.

Except inheritance is the premature optimisation of interfaces. Inheritance forces you to define the world in terms of strict tree hierarchies, which is very easy to get wrong. You may even do a great job today, but tomorrow such properties don't hold anymore. Regular composition allows the same functionality without making such strong assumptions on the data you are modelling.

Arborescent vs rhizomatic approaches, to get philosophical.

Unfortunately Deleuze's The Fold has little to say about either awk or catamorphisms.

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

#152

I love the way people take their narrow range of experience and over generalize it to everything. Bonus points for having a blog or maybe a patreon to tell everyone how it really is. We have a million flavors of the month but in the end it doesn't really matter. Pick a flavor and there will be teams that are successful and teams that are not. No silver bullet will make the non-successful teams magically turn into suc…

[deleted]

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

#154

It seems like there's a huge industry of people critiquing Java and OOP but misdiagnosing the problems as technical rather than sociological. The immense pain and suffering that is related to Java and its ecosystem is because of the terrible organizational conditions that tend to co-occur with usage of Java. Big slow companies, long boring meetings, arguments about design patterns, "architects" who haven't written co…

[deleted]

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

#155
post #63

Earlier quoted context omitted.

Have you ever seen wildly over-architected OO code where everything seems to be an abstract class and it seems impossible to find out where stuff actually happens? Inheritance is like a lot specialised tools - it can be useful in some situations but I think those are rarer than supporters might thing. Completely refusing to use inheritance and always using inheritance both seem like extreme views that should be avoid…

You haven't lived life to the fullest until you've had to debug an issue in a 12-layer-of-inheritance class with the original call ping-ponging a couple dozen times across the layers and overrides everywhere. I guess one could say this would be workable with proper tools, but the IDEs just aren't there. Move up a level in inheritance, ctrl-click on a call? It was overriden somewhere in the hierarchy, but the IDE will…

The person who wrote that would have made your life miserable if they had architected it 5 other different ways as well.

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

#156
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, other times where it's a reasonable tool, and other times it's a terrible tool.

Therefore "everyone uses it" because either a) it's a reasonable approach or b) the developer doesn't know of or can't use a better one.

We should work on fixing b) instead of denying a) exists.

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

#157

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

So many of these articles are written by people with no CS background who then had a couple stints short stints programming and then overnight pivoted to being expert consultants. They don't really have experience building or maintaining large complex systems. I am really curious who hires them. Most of them will have significantly less experience than the senior members of a successful team, and often less relevant…

No one is more confident in their abilities than a boot camp grad.

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

#158

I stopped using inheritance when Swift protocols and TypeScript interfaces came along. Although it sometimes means you need to do nasty things with generics, it beats the fragility of inheritance. With ObjC, I eventually came to hate updating superclasses, because I knew it would lead to unexpected behavior in subclasses.

Inheritance on interfaces is fantastic. Inheritance on implementations is fraught with peril. Java developers figured this out like 10 years ago.

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

#159

My situation is different than a lot of others--I work mostly with code-bases I 100% control--but I just don't use inheritance all that often, and if I do, it's never deeper than one or two levels. Now, I do most of my programming in Python and in JavaScript, and I do like using python's ABCs to specify interfaces for interfaces that I expect to be implemented, but that's mostly the end of it.

I remember the original reason for composition over inheritance to be very specific to Java: you can only inherit one class, but compose your class from multiple interfaces, each of which can have default implemented methods.

In python you've got polymorphism, mixins etc, so the reason for this suggestion doesn't really apply.

Django heavily uses inheritance for the controllers for example - and it works very well.

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

#160

Nobody calls it this, but cascading styles in CSS is just like inheritance and I think should be avoided for all the same reasons. I feel it's a big part of why CSS at scale becomes unmaintainable. There isn't even a built-in way to compose two classes together if you want to avoid cascading/inheritance. It looks like composition over inheritance has caught on as the better default in other languages, but in the CSS…

I really agree with this.

There was a company that I worked at 10ish years ago with a SaaS built around Drupal. They had one monolithic css file that was 25000 lines. It was a nightmare. When I took over the front-end rebuild, went through it line by tedious line and broke it out into discrete components.

I think about that from time to time when building logic or other functionality. I'd much rather have a self-contained bit of code than a big thing that is so intertwined I'm terrified to touch anything.

Post reply on HN