Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I'm so sick of this "composition is so much better than inheritance" talking point that seems to have become dogma in the last 5 years. You use both. They are completely different things.

Look at a classically inheritance-based system: a GUI library. You'll see lots of inheritance. Buttons and Checkboxes extend ClickableThing, ClickableThing extends Control. Whatever.

And then you compose those controls on a Form.

Look at a classically composition-based system: a video game entity-component system. You'll see Monsters composed of GraphicsObjects and Animations and WeaponSlots and AIAgents.

And you implement all of those components in an inheritance hierarchy, or else your GameObjectThinger can't have a list of them that can grow or shrink at runtime.

So keep saying "teach composition, not inheritance". You might as well tell musicians to study "rhythm, not tempo".



Those are good examples of inheritance. However, I think they'd be better served with mixins, which is another form of composition.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: