Live data from Hacker News

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

buttondown.email

211–220 of 389 posts

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

#211
post #121

Earlier quoted context omitted.

Ha ha, yes, happens all the time. In this case, published in a book 30 years ago, at least: https://news.ycombinator.com/item?id=40005520

Pretty sure it was in Effective Java and Design Patterns in Java 20+ years ago as well since Java is the language that gets picked on so frequently for this stuff.

He he. Java and patterns, Java and frameworks (mumble Spring mumble IoC mumble dependency injection), Java and factories and builders, mumble ...

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

#213

I don't think inheritance is bad at all. It is very often the easiest way by far to model a problem. Sure, it's not perfect, but I think it is wildly overhated by a vocal minority.

> I don't think inheritance is bad at all. It is very often the easiest way by far to model a problem.

I think the point of the article is that in some (very popular) languages, inheritance comes with a lot of baggage - precisely because classes in those languages support 3 different use cases. It's not saying that your usage is bad, but that your language didn't provide you with the best tool(s).

A lot of how people use classes can be solved via ADTs - which are much simpler to grok.

A lot of how people use classes can be solved via namespaces/modules, but not all languages have good support for them - so they use classes.

Etc.

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

#214
post #127

I only use inheritance for what the author calls ontological inheritance. For example, I find it useful to represent expressions and statements. I prefer composition if I'm trying to reuse data structures.

But ontological inheritance is the worst kind. Because your ontology is wrong. Similar things are kinda different. Different things are kinda similar. Your ontology is based on the linguistic remnants of taxonomic garbage from hundreds of years ago. There's no shelf.

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

#215

Earlier quoted context omitted.

I'm inclined to semi-agree with this, although I'm not sure I'd be quite as adamant about it myself. But I do generally think that CSS is taught in a way that encourages some bad practice around cascade/inheritance. I will point out though that (at least in my experience), BEM solved the majority of these problems for me even without a preprocessor. The language is definitely oriented towards inheritance/cascade, but…

> But I do generally think that CSS is taught in a way that encourages some bad practice around cascade/inheritance. I will point out though that (at least in my experience), BEM solved the majority of these problems for me even without a preprocessor. I think cascading is just a bad default, and I think methodologies like BEM agrees with this by teaching you ways to write CSS in ways that stops cascading from gettin…

> I think cascading is just a bad default

I'm again semi-inclined to agree, I just don't think I'd say it as forcefully; more that cascading styles tends to have a lot of downsides that people aren't familiar with and aren't taught.

My point isn't to badmouth Tailwind here; but debates about this sometimes boil down to "CSS purists" vs "Tailwind advocates" and my point is more -- nah, you don't have to like Tailwind to avoid the cascade. You can be a CSS purist and still avoid basic element selectors, your choice does not have to be either "do semantic styling targeting only semantic elements" or "jump on Tailwind and stick a bunch of styles inline."

I'm more sticking up for -- look, if you're someone who uses Tailwind, great, I don't have to tell you anything. You are already using a framework that (regardless of any other flaws it may or may not have) discourages you from using the cascade. But if you're someone who's in the position where you dislike CSS-in-JS or don't like using Tailwind, also great! I'm in that position too, I don't like Tailwind. But I still avoid cascade and basic element selectors and there are ways to basically eliminate most cascading styles from your codebase and eliminate most cascade-caused bugs even if you aren't going to use a pre-processor at all, and it's good to at least consider removing those cascading styles.

My only critique of Tailwind I would bring here is that sometimes I get the feeling that Tailwind advocates think that Tailwind invented this idea of component-based CSS, and it really didn't. But that's neither here nor there, and if someone is using Tailwind and it works for them, great. Life is way too short for me to argue with someone using a technology that they enjoy. Honestly, same with the cascade -- I think it can lead to long-term maintenance problems, but if you like it, fine.

However, if you're using CSS and hate it, and you also don't want to use Tailwind, then give BEM a try.

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

#216
post #40

Earlier quoted context omitted.

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

Yes, now take it further. Anyone not using components for all domain modeling is insane. Why does everyone assume this wonderful practice only applies to games?

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

#217

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.

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

#218

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…

Inheritance was a premature optimization for computers with small memory. It did its job. However, once memory got big, people forgot to throw it out.

Composition uses a lot more indirection. That's bad on modern CPUs. Pointer chasing throws out performance, so composition is not always preferred, either.

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

#219

Earlier quoted context omitted.

>It looks like composition over inheritance has caught on as the better default in other languages, but in the CSS world people still cling to the cascade as a best practice for some reason I find this falls into generally two camps, those who want to fight the browser and those who don’t. Those that tend to hate the cascade tend to fight the browser a lot, whether they realize it or not. Generally speaking, they wan…

> Those that tend to hate the cascade tend to fight the browser a lot, whether they realize it or not. Generally speaking, they want things to work a certain way (IE everything in isolation) and prefer to think of styles isolated bits. So, wouldn't that be the browser fighting them, then? They want something specific, and the broswer forces a paradigm upon them that they don't want. They are the humans and the browse…

This is a sane argument. I have no objection to it. Both things are true, in a sense.

It really depends on how you view the browser, IE should browsers be more adaptive to certain paradigms or should it set a reasonable paradigm and enforce it? I don’t know that there is a 100% right answer in this case, though they have moved to create better hooks for some forms of isolation (e.g. layers, scoping) but they fundamentally haven’t walked away from the cascade aspect.

It’s converging the two paradigms, for sure, but as I said, I don’t think either is wholly incorrect or correct.

Now if you wanted my opinion on the whole thing, I think the cascade is a fundamental element to be leveraged not avoided, but that’s me.

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

#220

Earlier quoted context omitted.

>It looks like composition over inheritance has caught on as the better default in other languages, but in the CSS world people still cling to the cascade as a best practice for some reason I find this falls into generally two camps, those who want to fight the browser and those who don’t. Those that tend to hate the cascade tend to fight the browser a lot, whether they realize it or not. Generally speaking, they wan…

> the second group often has a better grasp on keeping project maintainability over time in my experience. That's probably in the eye of the beholder and a necessity. If you are all in on the cascades then you have to limit the depth of your page structure, otherwise it becomes impossible to predict how things will ultimately render or what the impact of a change at layer 3 will have on the rest of the page. Technica…

Yes, there are more tools for isolation now, but you can’t wholly opt out of the cascade, even if it’s only local relevant, and I think that’s a good thing IMO
Post reply on HN