Live data from Hacker News

Inheritance was invented as a performance hack

catern.com

101–110 of 268 posts

Re: Inheritance was invented as a performance hack

#101
In my experience, 90% of the time people use inheritance but they really only cared about composition, and their language simply does not have any convenient facility to compose types and re-export their methods.

With a good type system that includes traits, you almost never need virtual dispatching, as any Rust developer may tell.

Re: Inheritance was invented as a performance hack

#102

After reading all the comments of many confused and curious, here is when inheritance is bad and why so _in the absence of any performance considerations_. First, inheritance from an interface/trait is totally okay. The problem is class inheritance, meaning implementation inheritance. There are two cases: 1) you inherit from a class and only add methods but don't overwrite anything. This is the good case, you can do…

This is only a problem is the methods on Stack are virtual. If they are non-virtual, inheritance works like your composition example: inheriting from Base basically creates a Base member variable, except missing methods can be automatically forwarded to the Base member. This saves you from having to explicitly wrap all the methods of Stack in CountingStack. Virtual should be used sparingly.

Agreed. Of the OOP languages I use, only C++ by default, and I think this is one of the few defaults in C++ that is correct. In Java terms, every method should be `final` except those that are explicitly intended as customization points.

Re: Inheritance was invented as a performance hack

#103

Pretty off topic comment, but I read the title as "inheriting money is a way avoid being a high performer", which happens to be true, although completely irrelevant to the actual article. ;)

> "inheriting money is a way avoid being a high performer", which happens to be true

By what metric? Or do you mean "a way to avoid having to be a high performer in order to achieve good results"? Isn't the whole beef with wealth inequality that given otherwise equal luck and faculties, a person who starts with more capital will gain more capital?

Re: Inheritance was invented as a performance hack

#104
post #23

Earlier quoted context omitted.

Why is code reuse your prime example for bad inheritance? What should people do instead?

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.

> Interfaces with default implementations are better than classes for a number of reasons

it's literally the same thing in practice

> including the diamond problem,

the diamond problem has only ever been a "problem" in OOP textbooks, in years and years of working on OO system I have never saw it be a problem in practice

Re: Inheritance was invented as a performance hack

#105
post #78

Earlier quoted context omitted.

> This problem is impossible to fix and it is a general problem - when overwriting a method, you can never be sure that semantics could break when the base class is changed. I'm not sure what's so special about overwriting methods here. There are many cases where semantics can break as a dependency changes. One example would be whether a callback is executed on the same thread (or event-loop tick) or in the backgroun…

Very good point actually. If we cannot assume synchronous execution, then semantics can break exactly as you said. That a rather orthogonal problem from the inheritance one and my suggested solution is to make use of pure functional programming (hence purity can be assumed unless the method signature indicates that an effect might happen). Here is an example of how a potentially async Stack might look like: class Sta…

You can push things into the type system only so far before you drop off a cost/benefit ratio cliff, unfortunately. Fundamentally, even in FP languages that expose side effects in the type system, you can still easily make APIs with undocumented semantics and in which users will break as the underlying component evolves. In fact Haskell is quite notorious for an ecosystem that seems to believe Haskell's type system obviates the need for good documentation, an illusion the Java world fortunately never labored under.

I think in recent years there's been an uptick in clever sounding attacks on common PL features like inheritance and exceptions, many of which look suspiciously like motivated reasoning. At the very least the arguments are extremely weak. This article ends by saying:

"Personally, for code reuse and extensibility, I prefer composition and modules."

But these are orthogonal. Languages like Kotlin have built-in support for inheritance, modules and composition. It's not an either/or approach, and there are lots of high quality, highly successful codebases that use inheritance extensively which would be pretty unimaginable without it. I use libraries that use inheritance every day and it's very rarely a problem: only in cases where someone made a bad API with it, and you can get bad APIs that rely on composition or badly modularised APIs too. I don't feel like one problem is more common than another.

Re: Inheritance was invented as a performance hack

#106
post #100

After reading all the comments of many confused and curious, here is when inheritance is bad and why so _in the absence of any performance considerations_. First, inheritance from an interface/trait is totally okay. The problem is class inheritance, meaning implementation inheritance. There are two cases: 1) you inherit from a class and only add methods but don't overwrite anything. This is the good case, you can do…

> This problem is impossible to fix People write working code using inheritance, it's not impossible, just requires extra care. One example: class CountingStack() { def push(element) returns nothing = count += 1; super.push(element) override def pop() returns element = count -= 1; super.pop(element) } Another way is to use non-virtual methods in the base class where you don't want people to modify your code. In C++ i…

Your first example will still break if the base class changes behavior, so I think my point holds.

I agree with the non-virtual methods though - they are pretty what I would call conceptual composition.

Re: Inheritance was invented as a performance hack

#107
post #103

Pretty off topic comment, but I read the title as "inheriting money is a way avoid being a high performer", which happens to be true, although completely irrelevant to the actual article. ;)

> "inheriting money is a way avoid being a high performer", which happens to be true By what metric? Or do you mean "a way to avoid having to be a high performer in order to achieve good results"? Isn't the whole beef with wealth inequality that given otherwise equal luck and faculties, a person who starts with more capital will gain more capital?

The two aren’t mutually exclusive. The children of the very wealthy often (usually?) share their parents’ economic advantage, thus perpetuating the inequality that originated due to economic factors. But that doesn’t mean they’re driven or that they’ll develop the same qualities that drove their parents to be successful.

Re: Inheritance was invented as a performance hack

#108

Pretty off topic comment, but I read the title as "inheriting money is a way avoid being a high performer", which happens to be true, although completely irrelevant to the actual article. ;)

I read it similarly as "inheriting money". And it's true that inheritance IS a performance hack--not for the children, but for the parents. Inheritance incentivizes parents to accumulate wealth not just for one lifetime but for multiple lifetimes. (Granted it may paradoxically have the opposite effect on the children as you mention.) Here's Milton Friedman on the matter: https://www.youtube.com/watch?v=km9OCw3f5w4

What's wrong with someone wanting to provide for his offspring?

Re: Inheritance was invented as a performance hack

#109

Earlier quoted context omitted.

I read it similarly as "inheriting money". And it's true that inheritance IS a performance hack--not for the children, but for the parents. Inheritance incentivizes parents to accumulate wealth not just for one lifetime but for multiple lifetimes. (Granted it may paradoxically have the opposite effect on the children as you mention.) Here's Milton Friedman on the matter: https://www.youtube.com/watch?v=km9OCw3f5w4

What's wrong with someone wanting to provide for his offspring?

Ted Kazinsky was talking about it in his manifesto.

https://unabombermanifesto.com/#THE%20POWER%20PROCESS

It depends on how much you provide for your offspring according to this deranged terrorist. And I've read stories from billionaire kids not living "the good life" because they never have to fight/work for anything.

Re: Inheritance was invented as a performance hack

#110

I think discussions of OOP and inheritance often miss the influence of Knowledge Representation on OOP: I’m not entirely clear of the history myself, but I’ve gotten the impression that knowledge representation research (things like frame systems) was a semi-independent influence on the design of OO systems

I think you might be looking for this https://en.wikipedia.org/wiki/Semantic_network

I believe it was Collins and Quinlan did the seminal work on the conceptual side, but what's interesting is it seems to emerge around the same time ... considering how inextricably bound the two are in computer science it's an interesting chicken/egg problem!

Post reply on HN