Live data from Hacker News

Inheritance was invented as a performance hack

catern.com

31–40 of 268 posts

Re: Inheritance was invented as a performance hack

#31

Earlier quoted context omitted.

"Catching on" doesn't mean "is the right way to do things". Code reuse is in fact a prime example of a misuse of inheritance

How is it a misuse? And what are the alternatives? To be clear, I do agree with your assertion, however I'm having a hard time actually putting "how/why" into words.

It’s a misuse because (if you really are only using it for code reuse) it’s creating an essential relationship between different objects that is actually incidental.

A list containing two different subclasses of your avoiding-copy-paste base class is now most likely a logical error.

Re: Inheritance was invented as a performance hack

#32
post #4

Fine. But obviously it caught on for other reasons, like code reuse and an intuitive mental model. The HN appetite for posts ragging on inheritance will never be sated.

What benefit does inheritance provide over importing files containing functions? Ruby and loads of other languages let you import files and those functions work like they were part of the class itself.

These are those different features. Delphi / ObjectPascal for example support either.

As for what benefit - your example with Ruby can be reversed and I can ask you the same question.

Anyways large part of a programming is an art and different people have different approaches. If you do not like the concept do not use it. Just do not assume that what you think is the "right way".

Re: Inheritance was invented as a performance hack

#33
post #21

I spent way too long trying to figure out how giving your kids your lifetime's earnings would be a performance hack beyond, you know, giving them a head start, before realizing what the headline was talking about.

People care tons about their kids -> Working hard and creating value results in a larger inheritance for their kids -> Exploit biological desires to protect offspring to get yourself to be more productive?

"Working hard and creating value" and "biological desires" are multiple inheritance, which is outside the scope of this article.

Re: Inheritance was invented as a performance hack

#34
post #23

Earlier quoted context omitted.

Interfaces with default implementations are better than classes for a number of reasons, including the diamond problem, and that as systems grow large they almost never fit cleanly into an inheritance tree. Fat pointers with two words, one for an interface vtable and one for the object, work really well.

In your view, how is "interfaces with default implementations" different from "inheritance for code reuse"? At a minimum it looks like a virtual function with a default implementation in the base class. If a derived class doesn't override it, isn't that code reuse?

Because interfaces don’t confer a relationship between implementations, only that they have some common method signatures.

Re: Inheritance was invented as a performance hack

#35
post #4

Fine. But obviously it caught on for other reasons, like code reuse and an intuitive mental model. The HN appetite for posts ragging on inheritance will never be sated.

Is this ragging on inheritance? I don't see much in the way of value judgements mentioned. The closest is an assertion that doing things for performance is not bad, which seems more like a way of heading off comments like yours than anything else. It's a bit of interesting history. I don't know why it would get a "Fine. But unrelated stuff" response.

Every once in a while one concept goes out of fashion and another one becomes current fetish. Often the new is actually an old one and sometime is masquerading under new name. And then you have army of prophets that are here to show you "the right way". Rinse and repeat. Personally I read and try various things and use whatever I think is appropriate for given situation. Do not really give a fuck about what is the official point of view on subject at the moment. It changes of course if doing job for client and client insists on things done in particular way. Luckily I mostly design and build complete products so clients mostly care about the end result and do not interfere in the internal plumbing matters.

Re: Inheritance was invented as a performance hack

#36
post #31

Earlier quoted context omitted.

How is it a misuse? And what are the alternatives? To be clear, I do agree with your assertion, however I'm having a hard time actually putting "how/why" into words.

It’s a misuse because (if you really are only using it for code reuse) it’s creating an essential relationship between different objects that is actually incidental. A list containing two different subclasses of your avoiding-copy-paste base class is now most likely a logical error.

[deleted]

Re: Inheritance was invented as a performance hack

#37
post #31

Earlier quoted context omitted.

How is it a misuse? And what are the alternatives? To be clear, I do agree with your assertion, however I'm having a hard time actually putting "how/why" into words.

It’s a misuse because (if you really are only using it for code reuse) it’s creating an essential relationship between different objects that is actually incidental. A list containing two different subclasses of your avoiding-copy-paste base class is now most likely a logical error.

how is that different from a list containing 2 implementations of an interface?

Re: Inheritance was invented as a performance hack

#38
post #23

Earlier quoted context omitted.

Interfaces with default implementations are better than classes for a number of reasons, including the diamond problem, and that as systems grow large they almost never fit cleanly into an inheritance tree. Fat pointers with two words, one for an interface vtable and one for the object, work really well.

In your view, how is "interfaces with default implementations" different from "inheritance for code reuse"? At a minimum it looks like a virtual function with a default implementation in the base class. If a derived class doesn't override it, isn't that code reuse?

Maybe he meant something similar to Go interfaces, but they are abstract and coupled loosely with implementations after the fact.

That or abstract class/pure interface. There's no need for default implementations introducing assumptions in code.

Re: Inheritance was invented as a performance hack

#39
tl;dr:

> Simula created inheritance instead of using composition because it allowed their garbage collector to be simpler.

In C++, inheritance (and other OO languages I assume) is implemented as a composition and when multiple inheritance of the same class happens then the members of the twice parent is inherited twice unless the virtual keyword is used. ( in other words, if B : A, A, C then sizeof B = A + A + C + B's stuff, or if B : virtual A, virtual A, C then sizeof B = A + C + B's stuff). This is why declaring on the stack Parent parent = Child() "works" by just truncating the content of the child that was appended at the end of Parent.

Personally I like to use private inheritance as a composition shortcut since for all intent and purpose it's achieves the same goal with less boilerplate. Outside of that I do use it if it strictly follows the substitution principle with other the members of the base class being used just as much as the child's. Which happens very rarely but does happen. It's like an Allen's key, you probably won't need it except when you must disassembled your IKEA furniture when moving out.

Re: Inheritance was invented as a performance hack

#40
post #21

I spent way too long trying to figure out how giving your kids your lifetime's earnings would be a performance hack beyond, you know, giving them a head start, before realizing what the headline was talking about.

People care tons about their kids -> Working hard and creating value results in a larger inheritance for their kids -> Exploit biological desires to protect offspring to get yourself to be more productive?

You just invented society!
Post reply on HN