r/programming • • Sep 24 '15

Composition over Inheritance

https://medium.com/humans-create-software/composition-over-inheritance-cb6f88070205
92 Upvotes

48 comments sorted by

View all comments

Show parent comments

8

u/wcdha Sep 24 '15

Great point. Additionally, he is making the assumption that the traits are going to work the same way and require the same code, which is not always true. Maybe the requirement for a MurderRobotDog is to bark robotically, so you'd be back to needing two implementations of bark() anyway.

Like you say the different strategies need to be used appropriately to make the most simple, powerful and maintainable code.

1

u/Redstonefreedom Feb 20 '23

It's a bit of a strawman fallacy to say: "he is assuming there will be no code change". In fact, this code as written, to me, would seem like it was made with the opposite assumption.

The difference being, upon change-request, instead of disinheriting & potentially losing all the coupled functionality in the inheritance chain, you'd just have to (1) copy & rename a function, and (2) change that single function call in the class of interest.

This isn't composition, but it also isn't inheritance. It's inherently less-coupled and is thus easier to change. It's not my pattern of choice, but it just isn't accurate of you to say it's more brittle.

These kinds of distinctions miss the greater point imo.