Live data from Hacker News

Inheritance was invented as a performance hack

catern.com

91–100 of 268 posts

Re: Inheritance was invented as a performance hack

#91

Earlier quoted context omitted.

This article discusses object/class inheritance in programming languages, not wealth inheritance between people.

There’s a difference?

In case you aren't joking, inheritance in the context of computer programming (for simplicity's sake) is just a metaphor that's supposed to make computer programming easier and has nothing to do with economics.

Re: Inheritance was invented as a performance hack

#92

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 the title as "new hypothesis about evolutionary theory and gene inheritance".

This is why I'm not a biologist (or OOP fan). :)

Re: Inheritance was invented as a performance hack

#93

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 thought the same and tbh was quite excited to read about that. Less interested in OOP.

Re: Inheritance was invented as a performance hack

#94
post #78

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

> 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 background. A function might assume that the callback should be pure and thus in a later version switch where it's being executed.

Rust's Send and Sync traits solve this particular problem by putting the thread-send safety in the type signature. If the library changes to use a background thread and you have passed it a non-threadsafe closure then you'll get a compile error.

Re: Inheritance was invented as a performance hack

#95
post #92

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 the title as "new hypothesis about evolutionary theory and gene inheritance". This is why I'm not a biologist (or OOP fan). :)

Omg I can't believe I didn't see it this way first, this is quite some title!

Re: Inheritance was invented as a performance hack

#96
post #89

Earlier quoted context omitted.

Theory usually comes after practice. People create something that works, then theorists formalizes it into a well defined concept.

I hold the view that programming patterns were invented as solutions to technical problems. It was then later that some theoretical "usefulness" or "elegance" was attached to them. One should know the technical benefits and drawbacks of programming patterns before applying them. Just because some language or environment or majority of programmers promotes it, doesn't mean that its ok to apply it. Without knowing the…

Many language features followed that route, yes. Languages themselves are typically first introduced, and then (maybe) formalized. Meanwhile, some other "patterns" or features, like higher-order functions or recursion, were originally introduced in the context of theoretical CS (e.g. they can be described in the lambda calculus), and it took some effort to implement them in practice.

Re: Inheritance was invented as a performance hack

#97
post #89

Earlier quoted context omitted.

Theory usually comes after practice. People create something that works, then theorists formalizes it into a well defined concept.

I hold the view that programming patterns were invented as solutions to technical problems. It was then later that some theoretical "usefulness" or "elegance" was attached to them. One should know the technical benefits and drawbacks of programming patterns before applying them. Just because some language or environment or majority of programmers promotes it, doesn't mean that its ok to apply it. Without knowing the…

I hold the view that programming patterns were invented as solutions to technical problems. It was then later that some theoretical "usefulness" or "elegance" was attached to them.

IIRC, there was impetus from Christopher Alexander's architectural "pattern language", and the desire to do something similar in software, which was a bit of an unguided mess at the time.

Re: Inheritance was invented as a performance hack

#98
post #89

Earlier quoted context omitted.

Theory usually comes after practice. People create something that works, then theorists formalizes it into a well defined concept.

I hold the view that programming patterns were invented as solutions to technical problems. It was then later that some theoretical "usefulness" or "elegance" was attached to them. One should know the technical benefits and drawbacks of programming patterns before applying them. Just because some language or environment or majority of programmers promotes it, doesn't mean that its ok to apply it. Without knowing the…

The other day I got into a heated debate with a C# developer on the merits (or lack thereof) of adding an "I" prefix to interfaces.

Turns out in C# there's no syntactic difference between implementing an interface and inheritance, so it makes sense in C# to explicitly state that something is an interface but, arguably, only there.

Re: Inheritance was invented as a performance hack

#99

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'…

Doesn’t Parent parent = Child() work because Parent’s constructor is run by implicitly casting ref-to-Child to ref-to-Parent?

I don't think that's enough, because Child() still results in an object that is sizeof(Child). In order to store that object in a variable that is sizeof(Parent) it has to be rejigged somehow.

I think what TeeMassive is saying is that this rejigging is a simple truncation because the memory layout of (non virtual) inheritance in C++ is a concatenation of Parent and Child objects. I'm not sure whether this is implementation specific though.

Re: Inheritance was invented as a performance hack

#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++ it's even the default. So you should consciously think when you make a method virtual.

    class Stack() {
       def nonvirtual private push_internal(element) returns nothing = ...
       def push(element) returns nothing = push_internal(element)
       def pushAll(elementlist) returns nothing = ... // use push_internal if needed
       def pop() returns element = ...
    }
Anyway - I agree inheritance is bad, and I dislike OOP in general, just wanted to nitpick.

IMHO the main problem is encapsulation. People expect the code to work one way and don't check their assumptions because of the whole tower of abstractions and hiding what actually happens behind layers. And then instead of writing straightforward code to do what has to be done - you have to tiptoe around all possible implementations :)

Post reply on HN