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…
If Inheritance is so bad, why does everyone use it?
351–360 of 389 posts
Re: If Inheritance is so bad, why does everyone use it?
#352Earlier 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.
Re: If Inheritance is so bad, why does everyone use it?
#353Earlier 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.
Re: If Inheritance is so bad, why does everyone use it?
#354Earlier 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…
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?
#355Earlier 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…
Re: If Inheritance is so bad, why does everyone use it?
#356Earlier 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?
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?
#357Earlier 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.
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?
#358The 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.
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?
#359Earlier 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
Re: If Inheritance is so bad, why does everyone use it?
#360I 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?
https://www.cs.virginia.edu/~evans/cs655/readings/smalltalk....