this post was submitted on 03 Jul 2023
103 points (96.4% liked)

Programming

17496 readers
38 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 4 points 1 year ago* (last edited 1 year ago)

There are some infamous cases of OOP abuse in the 7 digit LOC embedded system codebase I work in. I have known several developers that created these sorts of overly-engineered inheritance hierarchies seemingly just for their own sake. It's awful, and it's even worse when the original author leaves the company and leaves these sorts of unmaintainable blocs in our applications.

But I think there are definitely places where OOP is not only beneficial but just the correct solution for a part of the application. GUI for instance--all the various widget types and how they plug in to the UI system to handle mouse events and get drawn in the correct Z-order--it's very intuitive. Of course you will find a subclass of a subclass of a subclass of Button every once in a while, and in these cases I do look for opportunities to use composition over inheritance.

By the way - can we talk about the author's weird definition of composition? I've always defined composition as a class with a "has-a" relationship with other classes. But this author still defines it as classes with a "is-a" relationship to other classes, or in this case a single "generation" of inheritance. That seems bizarre to me, especially when they give this example:

In composition, this is a simple task: go to Dog class, add getTreat call in between eating and pooping. Done. ๐Ÿ’ช

And yet, their example of a Dog class following composition literally inherits from the Animal interface and does not have a point in which Dog can, internally, insert this getTreat() call:

class Dog implements Animal { doStuff() { consumeEnergy() println("woof") rest() } }

Lol... anyway, I like the sentiment of the article but I lean much farther toward just picking the correct tool for the job. Inheritance can still be the right solution in many cases and blaming a paradigm for code readability problems doesn't seem as apt as, say, realizing that even in codebases where every commit has to be reviewed, at the end of the day you still have to ship product and there is never time to keep all code in perfect order, especially in large legacy systems*.

  • note this may not apply to some open source projects or other projects where True Idealism can be/is exercised