Live data from Hacker News

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

buttondown.email

191–200 of 389 posts

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

#191

I think it is pretty clear that the reason inheritance is so prevalent is because it is a superficially good sounding idea, which again and again is being put in front of people. I am sure that many, many people have had the experience of a professor tell them a nice sounding story about purely hierarchical data and went on thinking that this is the way data should be modeled. Thankfully I believe that nowadays many…

This is exactly what I've seen and I think a lot came from java and C++ before it. Ironically C++ is a fantastic language now to use now while avoiding inheritance.

In the late 90s, 2000s and maybe even now, people see inheritance and think that it makes sense. They make the vehicle, the car, the sedan and then get stuck, when all they really needed was an x and y point and a velocity.

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

#192

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…

>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 browser (well, the styling language of the browser) is the tool that should accomondate them, not the other way around.

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

#193
post #182

Earlier quoted context omitted.

> what is the point of creating a language where everything is an object? I think that's the ultimate culprit in everyone hating inheritance. If it weren't for Java, I think we'd all have a healthier view of OO in general. I learned OO with C++ (pre C++-11), and now I work at a Java shop, but I'm luck that I get to write R&D code in whatever I need to, and I spend most of my time in Python. In C++ and Python, you get…

C++ templates are a form of duck typing as well, and combined with type erasure give you a lot of the benefits of OO without the downsides.

Yes, you're right. I've done that in high-performance code where I couldn't afford the double function call of a virtual function. I forgot about that.

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

#194

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…

> there's a huge industry of people critiquing Java and OOP but misdiagnosing the problems as technical rather than sociological.

I totally agree. I have worked on applications that would have collapsed under their own weight if it wasn't for Java. The strong type system, stable runtime and standard library APIs, as well as great tooling allows code to live far longer than it would in some other language ecosystems.

However, don't make the mistake of dismissing all of the criticism. The truth is often nuanced, and OOP/Java does have some serious downsides and footguns. There is room for both criticism and praise.

Ultimately, great systems aren't created by throwing a language and problems at a collection of random people. Great systems happen when good decisions are consistently made over time, and those are context dependent.

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

#195

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…

Prefer IS-A relationships where they make sense, i.e. where you expect the Liskov Substitution Principle to apply. Otherwise use composition. It's really that simple.

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

#196
post #80

It seems like protocols solve 90% of the problems that inheritance does, but with 10% of the headaches. Instead of trying to ensure that two types can both be passed to a function that only knows about the parent type, just have a parameter that says "Whatever's passed has to conform to this, I don't care what it is otherwise."

I'm guessing the closest equivalent in languages like Java or C# are interfaces, right? Underutilized IMO.

Interfaces in C# are used way more often than abstract classes and inheritance. It is also becoming more important as there is more code written in Rust style with structs implementing interfaces for zero-cost dispatch through generic constraints e.g.

    void Handle(T value) where T : IFeature1, IFeature2...

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

#197
post #142

Earlier quoted context omitted.

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…

>You can’t build a dynamic list of objects implementing the same interface in different ways with parametric polymorphism. Yes you can. that's the whole point of type classes.

Type classes are not parametric polymorphism.

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

#198
post #146

Earlier quoted context omitted.

“Protocols” is Apple-specific (or Objective-C/Swift-specific) terminology. They correspond to types 1 and 2 of inheritance that TFA mentions.

Protocols as a term have other prior art. Which shouldn’t be surprising because a protocol is also descriptive of interface boundaries between software products (such as, but not limited to, network protocols).

The term is unintuitive to me in that usage, because to me a protocol implies an exchange or a sequence of steps between two or more parties, such as in network or cryptographic protocol or a diplomatic protocol. Interfaces, however, only specify one endpoint of an interchange, in an inherently asymmetric way, and they primarily specify operation signatures, where there are often few constraints on the possible sequence of operations.

An interface can specify or be part of a protocol as a special case, but to me the term doesn’t match the general case.

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

#199

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…

>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.

Technically speaking, isolation is perfectly possible with webcomponents.

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

#200
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…

why is monster vs ageable mob a good cleavage? This is odd
Post reply on HN