Live data from Hacker News

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

buttondown.email

291–300 of 389 posts

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

#291

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…

It's because people don't understand cascade we end up with pages that have text in 18 different font sizes. Cascade was a brilliantly simple idea and gave great consistency of design easily. At the time it was great. I believe the only reason most people are mad at CSS is because specificity is hard to grasp and order-dependent resolution and you don't control it when you bundle 500 packages. You get scores of megab…

And not to forget that this also comes from a discrepancy between the fact a lot of people start with a design and want a CSS that matches that design, when CSS was made more as a theme system where you make a theme and let it handle placement and design. The result is clunky complex CSS to fight the browser into placing things an exact way and giving each element a different look.

In my opinion, this was a failure of communication from the beginning, with a huge divide between "I want my website to look like this because I drew it on paper with my designer friend" and "css is a cool new way of doing decent programmatic ux/ui.

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

#292
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."

I'm guessing the closest equivalent in languages like Java or C# are interfaces, right? Underutilized IMO.

In C++, you can use C++20 concepts[0].

[0] -- https://en.cppreference.com/w/cpp/language/constraints

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

#294
post #216

Earlier quoted context omitted.

Anyone not using components for game objects is insane. How do you add a fire breathing horse without that

Yes, now take it further. Anyone not using components for all domain modeling is insane. Why does everyone assume this wonderful practice only applies to games?

Because the negative consequences are more pronounced in games. You can more easily get away with not doing it in other domains, so there it may be silly, but not "insane".

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

#295
post #182

Earlier quoted context omitted.

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.

Yes, you're right. I've done that in high-performance code where I couldn't afford the double function call of a virtual function. I forgot about that.

Likewise this is getting easier with the auto keyword now being sugar for templates when used in function arguments and return values

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

#296
post #75

Earlier quoted context omitted.

> But I've never seen any coherent and sound arguments against OOP in general. How about arguments for OOP? Especially for things which can't be done better elsewhere.

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?

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

#297
post #286

Earlier quoted context omitted.

I think GP is talking about something like this: Fruit* fruit = new Apple(); ConsumeApple(fruit); // Doesn't work; requires Apple* fruit = new Banana(); ConsumeBanana(fruit); // Doesn't work; requires Banana* ConsumeFruit(fruit); // Okay, function signature is void(Fruit*)

But why would you want to be able to pass an apple to a function expecting a banana? Doesn't even make sense, the whole point is that the type system forces you to consider this stuff. If OP wanted to be allowed to do whatever and just have the software fail at runtime, JS is right there.

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

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

#298

Earlier quoted context omitted.

I'd ask what you mean by "fighting the browser"- as generally, the number one way to ruin the performance of your CSS is to introduce depth to it. In general, keeping everything isolated regularly leads to better rendering performance.

Avoiding the cascade at all costs, for example. It can introduce a lot of unintended consequences. Another anti pattern I have seen is the over use of media queries to force the browser to do certain things rather than embracing relative sizing constraints via intrinsic design and letting the flexbox and grid algorithms do most of the heavy lifting. Here though I want to point out isolation is relative, as is the cas…

To be fair, there was a time that flexion did not exist and extensive use of media queries was the only way to create a responsive website.

Those efforts don't just dissappear quickly, since that was the only way available that worked across all browsers for nearly a decade.

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

#299
We don't use inheritance anymore. In all the years building small and hughe software solutions with small and larger teams I've never experience any real scaling, maintenance or resilience benefit. Even for documentation it is more a pain for new devs onboarded than an advance. Patterns,DRY, DI, IOC, MS, ES, IO are best to build with these scopes in head.

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

#300
post #232

Earlier quoted context omitted.

Feels like that time my physics PhD student girlfriend asked me "hey you're a programmer, right" and I was one until I discovered ROOT and suddenly I lost taste for life and anything related to Computers.

Did you become a reclusive mathematician like Perelman or Grothendieck??

More like changed girlfriends...
Post reply on HN