6 ms·
> It's pretty good at modelling reality and such hierarchies are useful. There's just one problem: code should not deal with a model of the world. It should d
by loup-vaillant 2y ago
> It's pretty good at modelling reality and such hierarchies are useful.
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.
- mike_hearn 2y agoI gave other examples for "etc", like game engines. Plenty of others out there. Web server frameworks and so on also use 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.