by whstl · 2y ago · 0 comments
First, examples are often unpractical and divorced from reality. The canonical meme examples of “Dog and Cat inherit from Animal” or “Car inherits from Vehicle” are not really applicable to Veterinary Clinic Software or Self Driving Cars. People use those kinds of examples to say that OOP is good at modeling reality, but business software is 90% of the time better served by a different level of abstraction.
Second, a lot of descriptive information is passed as prescriptive. Patterns are a good example of how shoehorning concepts in a piece of software without having the knowledge to come up with it by yourself can make it worse, and yet teachers spend whole semesters dedicated to it.
Third, inheritance is still being taught as a fundamental part of OOP from the beginning in every curriculum I have seen, only to have pretty much everyone else saying “prefer composition” down the line, after the damage is already done.
Of all the topics that CS programs teach, it makes intuitive sense that the practical ones in software engineering are the ones most likely to be taught in a way which is irrelevant or backwards. Academia is good at academic things, but its members are typically there in part because they do not have a calling towards practice.
Perhaps the practical courses shouldn’t exist at all, but it’s tempting to marry the department with industry in some way. We don’t require philosophy majors to take courses in legal practice or whatever job they might take after graduation, because education is one thing and a career is another.
I had Pascal, Assembly and Haskell and it was quite practical and focused on universal concepts that apply to other paradigms and to software in general, it’s just OOP that has focus on concepts that are considered superfluous or damaging.
I personally blame fashion. Patterns and inheritance became fashionable and people just assumed they are as foundational as things like “variables” or “functions” or “ifs”.
Other frameworks—-like jQuery, Express, or Handlebars—-get unfairly short-shrifted in the curriculum.
One of the systemic dynamics with OOP is that Oracle invested itself into Java and also funds academic research. This might create a conflict of interest in designing the curriculum, but I don’t know if there are any checks for that.
The argument here is not so much that "implementation inheritance doesn't work at all" but rather that it is a limiting way of modeling that causes more problems than it solves, and teaching it is a problem in itself.
Implementation inheritance should be a footnote given with a warning, rather than a main technique.
Dependency injection is not OOP. When used in an OOP context, it just solves an issue created by OOP. In an OOP context you can't call code from another object unless you have a reference to it. So you either instantiate the object or use DI to supply an object reference so you can call a method.
In contrast to that, if you have functions as first class citizens or your code resides in a static class, you seldom need DI because you can call the function or method from wherever you need.
Plus teaching proper separation of concerns rather than the "one line per method" from Clean Code.
All this would lead to better software than the "Dog inherits from Cat" bs.
https://dev.epicgames.com/documentation/en-us/unreal-engine/...
The reason we don't tend to see such hierarchies used in clinic software is that RDBMS engines aren't OOP and don't model inheritance hierarchies well.
Self driving cars are big piles of C++ so probably have plenty of OOP, albeit given the ML bent probably more for dealing with UI and sensors than modelling the nearby landscape.
Inheritance is taught because most real OOP codebases use it extensively and it works OK. If you don't understand inheritance you can't understand the standard libraries of most OO languages, most UI toolkits etc.
The industry (and this includes Unreal, albeit slowly) has been moving to runtime composition for a reason: it is simpler and more flexible. And much better at modeling reality.
Your last paragraph pretty much sums it up: the main reason at this point for teaching implementation inheritance is because of legacy code bases. It can be a valid technique (I personally enjoy the Template Method Pattern, although it is today a maligned pattern) but it does causes more problems than it solves.
https://dev.epicgames.com/documentation/en-us/unreal-engine/...
But more importantly, is this take falsifiable? What does "old" or "legacy" mean? People have been pushing this line for at least 15 years here on HN, yet what we see in the most well funded and actively maintained codebases is lots and lots of inheritance, with no efforts to remove it. Not just Unreal but also Chrome, MS Office, iOS, Android, Java, and more, all use this technique with no ill effects as far as anyone can tell. When the maintainers talk about what issues they face and are putting refactoring efforts into, inheritance or OOP never seem to be on the list. In the Java case it's actually the opposite, they like to complain about people violating OOP encapsulation and want to make enforcement stricter. Meanwhile heavily hyped successors that lacked it, like Haskell, have vanished without a trace, leaving not even one widely used program in their wake.
What would it take to falsify the claim that inheritance is a legacy technique? Because I see no real evidence of it. Every codebase I've worked on has used it without anyone remarking on that fact, and it didn't seem to cause issues more often than other design patterns.
Haskell still is a thing, and Pandoc and shellcheck are widely used, if you aren't, you're missing out.
And Haskell impressed Tim Sweeney enough that a whole bunch of Haskell people are working on Epic's Verse language.
Do you think that inheritance had something to do with that?
The reason Unreal uses inheritance is because this is what people did in 1998. The reason it can't stop using is because it's too late to change. There's nothing more to it.
A demonstration that inheritance is a good technique. Which it is not. It hurts locality of behaviour, and that's bad because it increases the rate of mistakes. There have been studies about this.
There's just one problem: code should not deal with a model of the world. It should deal with a model of the data.
A chair is a chair you'd say? Well it depends. Is it a background texture you'd never get close enough to notice it's flat? Is it a static mesh that never moves, and could be considered part of the ground itself? Is it something you can grab and displace? Can it be swung like club, or thrown like a rock, and do damage if it hit you or an NPC?
Depending on what you want to do with that chair, the data describing it will be very different. And if you have any performance constraint that data will likely be put in very different structures or systems, from landscapes to two-handed weapons. The very concept of "chair" at this point is mostly moot.
> Inheritance is taught because most real OOP codebases use it extensively and it works OK.
Avoiding inheritance would have worked better in most such codebases. I have seen things…
> If you don't understand inheritance you can't understand the standard libraries of most OO languages, most UI toolkits etc.
Any item behind "etc"? Because so far those two are the only example of inheritance being kind of mandatory. And I'm not even sure about standard libraries, the C++ STL is very light on inheritance, and I see very little of it in Python (at least as a user). I guess Java makes heavy use of it.
Feel free to argue with the Unreal devs that they're doing it all wrong, or make a competing game engine that never uses inheritance. Whilst "chair" is not something you'll see in their C++ code (too specific), objects like players, static meshes, lights etc are and I guess it works fine for them.
Avoiding inheritance doesn't necessarily work better. I too have seen things, and what you find in languages that don't have inheritance is that they often end up growing libraries that emulate it, just in hacky ways and far too late. That's pretty common in C codebases.
As a simple example of the problems anti-OOP ideology creates, try using Jetpack Compose (a sort of React on steroids) to make a UI where later you decide you want a particular customization to every instance of a particular control in your app. Have fun... the way you customize anything is to wrap that composable in another composable and manually forward all the properties. Want to change the way buttons work? Find every place where a button is used and replace it with the name of your new button, then keep your new MyButton up to date as the underlying library adds new properties. Hope there are enough callbacks to do what you need, and try to avoid being distracted by them all when you're just using a button instead of customizing it. Want to write code that's generic over certain kinds of controls? You just ... can't ... because controls aren't objects now, they're functions. You end up manually implementing inheritance without the aid of the compiler or language features like visibility modifiers. It's far from ideal.