Live data from Hacker News

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

buttondown.email

181–190 of 389 posts

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

#181

There’s a rule-of-thumb I’ve found about OOP where you want to use composition when extending data, and you want to use inheritance when extending behaviors. The zoo animal metaphor that you often see used in teaching inheritance (lions are animals, simba is a lion all animals have a length, but lions also have claws etc.) is a bad perspective and you should never use inheritance to model a problem like that in the r…

> The only time I’ve used inheritance, has been on toy problems that didn’t need it

See https://news.ycombinator.com/item?id=39999644

It's very useful when a base class provides an incomplete implementation, and the child class completes the implementation.

Sometimes I use it when testing, where the child class alters some behavior in a way that mocking can't do correctly.

In C# / Java, inheritance is used for setting up filters for exceptions

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

#182

Earlier quoted context omitted.

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?

> what is the point of creating a language where everything is an object? I think that's the ultimate culprit in everyone hating inheritance. If it weren't for Java, I think we'd all have a healthier view of OO in general. I learned OO with C++ (pre C++-11), and now I work at a Java shop, but I'm luck that I get to write R&D code in whatever I need to, and I spend most of my time in Python. In C++ and Python, you get…

C++ templates are a form of duck typing as well, and combined with type erasure give you a lot of the benefits of OO without the downsides.

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

#183
post #79
post #59

My impression is that inheritance is in many common programming languages the easiest way to share code. Sharing code in another way would require some more thoughts and sometimes code, so the lazy programmer takes inheritance. Traits as a general concept would be very useful in many programming languages, but only some (like Scala) have proper support for it. In principle a Java interface with default methods (imple…

Traits and contracts or interfaces, but even still inheritance has it's merits. Being fast and easy is a benefit, and I find defensive programming in extensible systems can benefit from a foundation of expecting inheritance.

What do you mean by this sort of defensiveness? Can you give an example?

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

#184

I stopped using inheritance when Swift protocols and TypeScript interfaces came along. Although it sometimes means you need to do nasty things with generics, it beats the fragility of inheritance. With ObjC, I eventually came to hate updating superclasses, because I knew it would lead to unexpected behavior in subclasses.

Despite misgivings about the language as a whole, I think that Go does interfaces extremely well, and made the right choice to discard "implementation inheritance".

That said, in my time with obj-c I never really encountered the problem of fragile base-classes - protocols and categories gave us just about everything we needed. (the foundation classes use implementation inheritance extensively, but we don't typically modify those too often :) )

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

#185

I don't think inheritance is bad at all. It is very often the easiest way by far to model a problem. Sure, it's not perfect, but I think it is wildly overhated by a vocal minority.

Have you ever seen wildly over-architected OO code where everything seems to be an abstract class and it seems impossible to find out where stuff actually happens? Inheritance is like a lot specialised tools - it can be useful in some situations but I think those are rarer than supporters might thing. Completely refusing to use inheritance and always using inheritance both seem like extreme views that should be avoid…

Sure, but I have also seen C code where random structs with function pointers are passed to god-knows-where, and I will take inheritance over that any time of the day.

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

#186

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…

I feel like Clojure-style multimethods accomplish this better than inheritance. I can simply write a dispatcher that dispatches the correct function based on some kind of input.

This is evaluated at runtime, thus giving me the runtime polymorphism, but doesn't make me muck with any kind of taxonomy trees. I can also add my own methods that work with the dispatcher, without having to modify the original code. I don't feel like it's any less convenient than inheritance, and it can be a lot more flexible. That said, I suspect it performs worse, so pick your poison.

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

#187

I stopped using inheritance when Swift protocols and TypeScript interfaces came along. Although it sometimes means you need to do nasty things with generics, it beats the fragility of inheritance. With ObjC, I eventually came to hate updating superclasses, because I knew it would lead to unexpected behavior in subclasses.

I had the same experience. I’m a few years into using rust now - which doesn’t implement inheritance at all and I don’t miss it. In typescript I barely use classes at all. I prefer an interface paired with a constructor function. I think it’s almost always better to first think of data as a struct, with associated code that acts on that struct. As the old saying goes, show me your data structures and not your code, a…

Rust implements interface inheritance out of the box via traits, but you can also replicate something very much like implementation inheritance via phantom types/the typestate pattern. It just finds very little use in general, because it's readily apparent just how clunky that whole arrangement is.

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

#188

Boring answer: Same reason we do a lot of stupid things, haha. “If C is so unsafe and C++ is so unwieldy why does anyone use them?” Because that’s what they learned, or because that's how the codebase is structured when you get there. When all you have is a bike you’re going to ride that shit everywhere. You rarely if ever get the chance to start from scratch at work (where most code is written) and your job is to fo…

Main reason I use c++ is because it has the most mature libraries. I gave Rust a try, and lots of nice things about it. But many of its libraries are not very mature.

What libraries do you miss from C++ that you can’t find in rust? ML is the big piece for me - there’s no mature equivalent to CUDA or PyTorch.

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

#189
post #142

Earlier quoted context omitted.

You can’t build a dynamic list of objects implementing the same interface in different ways with parametric polymorphism. As another example, the Unix file interface ( open() , read() , write() , flush() , close() ) etc. is an example of runtime polymorphism, where the file descriptors identify objects with different implementations (depending on whether it’s a regular file, a directory, a pipe, a symbolic link, a de…

>You can’t build a dynamic list of objects implementing the same interface in different ways with parametric polymorphism. Yes you can. that's the whole point of type classes.

Not without runtime polymorphism. Parametric polymorphism does not imply nor by itself implement runtime polymorphism. I.e. C++ templates, or generics in other languages, provide parametric polymorphism, but not runtime polymorphism.

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

#190

Earlier quoted context omitted.

Main reason I use c++ is because it has the most mature libraries. I gave Rust a try, and lots of nice things about it. But many of its libraries are not very mature.

What libraries do you miss from C++ that you can’t find in rust? ML is the big piece for me - there’s no mature equivalent to CUDA or PyTorch.

Any kind of usable GUI library. Plus I am doing 3d graphics, and rust is not there yet.
Post reply on HN