Live data from Hacker News

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

buttondown.email

341–350 of 389 posts

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

#341
post #73

Earlier quoted context omitted.

> 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. The runtime part is what I dislike. If I have a fruit which is an apple or a banana, I can't pass that to a method expecting an apple or banana. It can only be passed as a fruit. > And if you want to avoid huge if-else statements, you should pu…

> I prefer to have all the type-checking code inside the TypeCheck module There are ways to have your cake and eat it too, here, at least in some languages and patterns. For example, in Go you could define "CheckType" as part of the interface contract, but group all implementors' versions of that method in the same file, calling out to nearby private helper functions for common logic. Ruby's open classes and Rust's m…

> And yeah, sure, some folks will respond with "but go isn't OO", but that's silly. Modelling polymorphic behavior across different data-bearing objects which can be addressed as implementations of the same abstract type quacks, very loudly, like a duck in this case.

It's less "this is not OO" and more this is not inheritance, which is why a lot of people are saying you can find more elegant solutions (like this) rather than use inheritance for no clear benefit.

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

#342

There is no valid use case I've ever seen where inheritance was better than composition (that does not mean a use case does not exist, but evidence is mounting against it). I would say it's used because every language made a poor decision by building that in as the way to encapsulate reusable logic. i.e., a mistake. Some APIs may expose themselves as requiring you extend a base class, and in that case you might as we…

Composition implies a level of complexity. There may be a near infinite number of permutations of things that can be combined. If in reality there are only a fixed set of variations, inheritance may model your system in a tighter way. The benefit can be simplicity.

> Composition implies a level of complexity

How?

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

#343
post #305
post #80

It seems like protocols solve 90% of the problems that inheritance does, but with 10% of the headaches. Instead of trying to ensure that two types can both be passed to a function that only knows about the parent type, just have a parameter that says "Whatever's passed has to conform to this, I don't care what it is otherwise."

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 an object Foo that conforms to protocol Bar with method baz, you'd have to write obj->Bar->baz() as opposed to just obj->baz(). Or alternatively, Foo->Bar->baz(obj) if you want to make any given call site discoverable.

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

#344
post #63

Earlier quoted context omitted.

You haven't lived life to the fullest until you've had to debug an issue in a 12-layer-of-inheritance class with the original call ping-ponging a couple dozen times across the layers and overrides everywhere. I guess one could say this would be workable with proper tools, but the IDEs just aren't there. Move up a level in inheritance, ctrl-click on a call? It was overriden somewhere in the hierarchy, but the IDE will…

The person who wrote that would have made your life miserable if they had architected it 5 other different ways as well.

Disagree. Some things are just easier to get right than inheritance.

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

#345
Depends what language you're using and what polymorphism features it has...

C - You don't use inheritance because it literal has none, but you can handroll dynamic dispatch (interface style Abstract Base Classes)

Rust - Also doesn't have it and people seem perfectly happy with generics, sum-types, and `dyn Trait`

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

#346
post #314

Inheritance is really just a public interface, a private interface and automatic delegation of those interfaces to the base class. The problems with inheritance are: - people shove code in the base class to dedup it without thinking about design - people add public and protected methods without thinking about interface design - the names of the base class and the public and protected interfaces are exactly the same t…

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.

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

#347

Inheritance is really just a public interface, a private interface and automatic delegation of those interfaces to the base class. The problems with inheritance are: - people shove code in the base class to dedup it without thinking about design - people add public and protected methods without thinking about interface design - the names of the base class and the public and protected interfaces are exactly the same t…

> If base classes were only allowed to be used in inheritance and couldn't be parameters, generics, etc then that would force users to create base classes and public interfaces in pairs

Which, if its a common thing, suggests that there should be syntax for like:

  interface IFoo from FooBase;
That makes an interface from the public members of a class.

Or, perhaps even better, type systems should enablr.a distinction between:

class ConcreteFoo inherits FooBase; // normal implementation inheritance class ConcreteFoo implements FooBase; // is required to (compiler checked) present the public interface of FooBase, and is type-compatible, but doesn't inherit implementation

So concrete classes can also serve as interfaces, and purely abstract classes are identical to interfaces.

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

#348

Earlier quoted context omitted.

Do you have a CSS component framework that you can recommend for use with BEM?

I could link you to a few (I think Bootstrap adopted BEM at some point, Material Design Lite I think uses a variant of it), but I can't recommend them with confidence because generally I don't use extensive style frameworks when I work on large applications. I feel like CSS frameworks are largely useful for bootstrapping projects and for keeping control of large projects where styling starts to break down. A lot of p…

That was really helpful and cleared up some notions I had. Thanks

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

#349
One thing that bothers me is when people refer to languages like Go and Rust as "not object-oriented". Of course they are object oriented; Go and Rust programs consist of struct definitions to which you associate methods, giving rise to individually instantiatable objects with custom behavior, and you use these objects to model entities in your problem domain. This is object-orientation.

Where they differ from OO languages such as Java and Python is of course that they avoid traditional inheritance, offering more restricted inheritance-adjacent features (typeclasses/traits/interfaces and struct embedding).

Like many others I find Go disappointing in many regards. (Although Go's channels, goroutines, select, and implicit interfaces are beautiful). One disappointment is the way that methods are not indented or nested within the struct definition/interface that they are defined on. This is, IMO, a silly fig-leaf that is attempting to make the language look "not OO" in a superficial manner, but all it ends up doing is making it hard to see where your implementation of one interface ends and another starts.

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

#350

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…

Nobody calls it this, because it is not. CSS classes have nothing to do with OOP other than the word "class".
Post reply on HN