Live data from Hacker News

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

buttondown.email

351–360 of 389 posts

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

#351

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…

CSS does actually call it inheritance[0], but it's commonly mixed up with the cascade. Inheritance applies to certain properties, so that when they are not specified on an element, an element inherits the value of the parent. The cascade[1] determines how rules from multiple sources are merged. [0]: https://developer.mozilla.org/en-US/docs/Web/CSS/Inheritance [1]: https://developer.mozilla.org/en-US/docs/Web/CSS/Casc…

CSS calls something completely different "inheritance". There is no class inheritance in CSS, because CSS classes are not OOP classes.

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

#352
post #302
post #297

Earlier quoted context omitted.

> But why would you want to be able to pass an apple to a function expecting a banana? I would never. I know that I'm holding a banana, I want to pass it to a method that receives a banana. But what I can't do is put my banana through a rotateFruit function first, because then Java will forget that it's a banana and start treating it as a fruit.

Java has generic functions since forever, something like ` T rotateFruit(T fruit)` would return your banana typed object just fine.

But then I lose polymorphic dispatch, which is where this thread started.

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

#353
post #352
post #302

Earlier quoted context omitted.

Java has generic functions since forever, something like ` T rotateFruit(T fruit)` would return your banana typed object just fine.

But then I lose polymorphic dispatch, which is where this thread started.

If you pass your object to another function, you only get static dispatch. What you want instead in this case, is a simple instance method. Then yourApple.rotateFruit() would do what you want, and rutateFruit could be an interface method declared in Apple, the superclass/interface.

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

#354
post #343
post #305

Earlier quoted context omitted.

Nice in principle, but doesn't it make it quite difficult to analyze such code? E.g. finding places that use a particular function of a protocol. I recall Pyright for Python is not able to find such call sites. But perhaps there are better implementations of the concept?

A lot of OOP syntax has that exact problem because you don't use a fully-qualified method name. That's actually an upside of a janky C pseudo-OOP patterns. You have to call a function, that function has one name, one way of being called. So you can usually find all the call sites with grep (modulo macro explanations or something). I could imagine a syntax that made you specify the protocol at the call site, e.g. for…

Indeed in OOP (or in any kind of indirect call mechanisms, including ones using function pointers in C, right?) you would not find which particular method is being called, but if you are using a nominative type system with OOP, you would at least find which method of which interface is being implemented—and similarly from the call site you would find which interface is being called. So you get a set of functions that may be called, and you would get the precise list of sites they may be called from. And this you get "for free".

In your proposal by stating the name of interface you are calling you do get to see what is being called, and some protocol implementations such as the one in Python/MyPy you can name the protocol of the object you are invoking a method on.

But how about the implementation sites? I was under the impression with protocol based system you would implement a protocol by just having methods of certain names. So the set of functions that may be called would be the set of classes that happen to have a certain set of methods that conform to a protocol, which might or might not be made with the idea that they should be used in the protocol's context. You would need to rely on documentation, which isn't checked by the compiler.

Personally I think protocols are too ad-hoc for general program structure purposes, though they can have their uses. I consider both "class implements these abstract named interfaces" and traits (as in Rust) better.

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

#355

Earlier quoted context omitted.

I agree with this sentiment but I also must say that the time's I've needed inheritance have been few and far between. I have seen really good examples where it works really well (UX is pretty common, but I've also seen really clean cases like data structures with complex interfaces and a simple abstract class). Where I think inheritance works best is when the state in base classes is limited and the interface is qui…

> I agree with this sentiment but I also must say that the time’s I’ve needed inheritance have been few and far between. You never strictly need inheritance, but strict need isn’t the criteria for whether something is a good solution (otherwise the fact that most things in programming can be done multiple ways would mean that almost nothing is ever a good solution, since there is almost always an alternative route to…

I assume the person didn't mean "need" as in "can't do without it", but instead as in "I can make clearer code with it than with the alternative approaches".

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

#356
post #296

Earlier quoted context omitted.

As others have said, GUIs are the place where OOP shines. There are no things OOP does that can't be done elsewhere, what happens in practice is that programmers invent their own ad hoc object systems and start programming in an object-passing style (see e.g. Gtk).

What is an object-passing style?

Whatever it takes to emulate object-oriented programming in languages without objects and classes. Creating structs as objects and passing them as first arguments. But I also meant it to include using closures, function pointers, and type-switches to emulate dynamic dispatch, creating inheritance hierarchies by embedding higher classed object into structs in whatever way your language supports. Sometimes people also resort to code generation. Even explicit dynamic dispatch may be implemented, e.g. by storing class hierarchies explicitly.

The hallmark of it is that "objects" are passed as first arguments to functions in languages that don't support syntactic sugar for methods.

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

#357
post #314

Earlier quoted context omitted.

It's worth mentioning regarding the IFoo example that in a single project you can always just find/replace FooBase with IFoo or even FooBaseV2. I feel like a lot of people blow this up like it's so e huge problem when in reality this is a pretty trivial refactor.

Not if you're writing a library/framework that other people use though. If all you do is consume other people's library/frameworks then yeah, nothing really matters, do whatever you want, refactor at will.

Yeah, that's kind of my point. I agree that api design is important if you're publishing a library, even for internal use - that's why I said "in a single project".

It seems to me that a lot of people obsess a bit too much about this stuff in places where it doesn't matter.

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

#358
post #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.

Simple composition, where an entity just has some components on it, can be completely "unrolled" at compile time, essentially removing all indirection as if the entity had all of the data to begin with. You don't need pointers.

For more complex composition, an ECS runtime would likely group all components of the same kind into the same place so that systems that ran with those components get even better data locality.

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

#359

Earlier quoted context omitted.

Would you happen to know any good literature with clear composition examples vs inheritance? The C++ and Python code I see daily is fully inheritance focused, and I would like to understand how it could be done differently (or better) starting from a perspective I understand.

https://gameprogrammingpatterns.com/component.html

Got a simpler example?

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

#360

I feel people don't understand what inheritance and (object orientation in general) is useful for, misuse it, and then it gets a bad reputation. It's not about making nice hierarchies of Cars, Fruits, and Ovals. For me the main point is (runtime) polymorphism. E.g. you have a function that takes a general type, and you can pass multiple specific types and it will do the right thing. And if you want to avoid huge if-e…

Polymorphism is doable in plain old C with lookup tables and function pointers. If that is the only benefit, what is the point of creating a language where everything is an object?

1981 "Design Principles Behind Smalltalk"

https://www.cs.virginia.edu/~evans/cs655/readings/smalltalk....

Post reply on HN