Live data from Hacker News

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

buttondown.email

91–100 of 389 posts

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

#92
post #40
post #33

The article has a nice history review of inheritance, but it would have been useful for there to be some concrete examples. The worst example of inheritance I've ever found is in java Minecraft's mobs[0]. It is very deep in some places and has numerous examples of sub classes not fully implementing their parent interfaces. Example 1: Donkey[1] Donkey > Chested_Horse > Abstract Horse > Animal > Ageable Mob > Pathfinde…

Both of those look like perfect cases for composition instead of inheritance.

Anyone not using components for game objects is insane. How do you add a fire breathing horse without that

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

#93

Do they? I rarely see people using it directly and I think this is fine

Most of us have no contact with Java. But Java offers little else than inheritance to use to organize a system, so Java coders use it for everything. They are not exactly wrong, except in using Java at all. But sometimes is all that is allowed.

java offers composition, like pretty much all programming languages

if you have a person class and you have students and teachers

the correct thing to to is NOT to make student and teacher inherit from person

it's to make student and teacher have a person attribute + the other attributes that constitute a student and teacher respectively

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

#94
post #73

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. And if you want to avoid huge if-e…

> For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. The runtime part is what I dislike. If I have a fruit which is an apple or a banana, I can't pass that to a method expecting an apple or banana. It can only be passed as a fruit. > And if you want to avoid huge if-else statements, you should pu…

> I prefer to have all the type-checking code inside the TypeCheck module

There are ways to have your cake and eat it too, here, at least in some languages and patterns.

For example, in Go you could define "CheckType" as part of the interface contract, but group all implementors' versions of that method in the same file, calling out to nearby private helper functions for common logic.

Ruby's open classes and Rust's multiple "impl" blocks can also achieve similar behavior.

And yeah, sure, some folks will respond with "but go isn't OO", but that's silly. Modelling polymorphic behavior across different data-bearing objects which can be addressed as implementations of the same abstract type quacks, very loudly, like a duck in this case.

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

#95

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…

Obligatory: Tailwind

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

#96

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…

Obligatory: Tailwind

I tried to avoid mentioning that, but Tailwind is showcasing how to write CSS using composition from inheritance. But it can't overcome the knee-jerk reaction from many CSS developers that don't see the problems that comes with CSS cascade/inheritance. I think this is mostly because leaning on cascading rules is seen as a best practice and most aren't going to question best practices much.

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

#97

It's not bad. People like to feel smug by saying OO is bad and hence inheritance, meanwhile using some construct in their FP that is really the same thing.

you seem to be misunderstanding different terms. You can do OO without inhertitence.

inheritance is the worst as you grip with you types.

just use strategy patterns instead.

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

#98

Earlier quoted context omitted.

Obligatory: Tailwind

I tried to avoid mentioning that, but Tailwind is showcasing how to write CSS using composition from inheritance. But it can't overcome the knee-jerk reaction from many CSS developers that don't see the problems that comes with CSS cascade/inheritance. I think this is mostly because leaning on cascading rules is seen as a best practice and most aren't going to question best practices much.

Tailwind is not a panacea—it has its own problems which withhold adoption, "kneejerk reactions" notwithstanding.

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

#100

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…

[deleted]
Post reply on HN