I'd just like to point out, the author isn't demonstrating Composition over Inheritance, but Multiple Inheritance over Single Inheritance. So the whole deliberation later in the video about when to use, or prefer composition over inheritance is kind of pointless, given he's confused about what composition is.
Merging objects together is essentially a form of multiple inheritance (or more casually: "traits" or "mix-ins"). When people talk about composition they mean one object having other objects as properties and using those "sub-objects" to achieve a higher goal.
The benefit of composition is that every object retains its encapsulation, and you eliminate the risk of collisions that's present when you merge objects (say Cat adds method "kill" and you merge it with Robot which has method "kill"... problem).
EDIT: And one more note. Because JS doesn't support true multiple inheritance, using Object.assign() has another implication, in that instanceof checks won't return true when checking an object against its parent types.
Whether this matters is up to your design, but I wouldn't give it up so easily and say it's useless. Inheritance, mix-ins and composition are three distinct strategies with distinct pros, cons and uses. The issues come from not knowing what their proper uses are.
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.
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.
85
u/[deleted] Sep 24 '15 edited Sep 24 '15
I'd just like to point out, the author isn't demonstrating Composition over Inheritance, but Multiple Inheritance over Single Inheritance. So the whole deliberation later in the video about when to use, or prefer composition over inheritance is kind of pointless, given he's confused about what composition is.
Merging objects together is essentially a form of multiple inheritance (or more casually: "traits" or "mix-ins"). When people talk about composition they mean one object having other objects as properties and using those "sub-objects" to achieve a higher goal.
The benefit of composition is that every object retains its encapsulation, and you eliminate the risk of collisions that's present when you merge objects (say Cat adds method "kill" and you merge it with Robot which has method "kill"... problem).
EDIT: And one more note. Because JS doesn't support true multiple inheritance, using Object.assign() has another implication, in that instanceof checks won't return true when checking an object against its parent types.
Whether this matters is up to your design, but I wouldn't give it up so easily and say it's useless. Inheritance, mix-ins and composition are three distinct strategies with distinct pros, cons and uses. The issues come from not knowing what their proper uses are.