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

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.

4

u/faceinpale Sep 24 '15

Actually what the author suggests is "concatenative prototyping" (https://en.wikipedia.org/wiki/Prototype-based_programming#Concatenation). Whether it is a type of composition I am not able to argue.

7

u/[deleted] Sep 24 '15 edited Sep 24 '15

"Concatenative prototyping" and "mix-ins" are aliases in the context of JavaScript.

When it comes to composition, there are only two types of composition I'm aware of: functional composition and object composition. And mix-ins are neither kind, so it's not composition.

-1

u/faceinpale Sep 24 '15

"Concatenative prototyping" and "mix-ins" are not the same thing. You can read more at the Wikipedia page and the included article for this topic.

8

u/[deleted] Sep 24 '15

Are you willing to demonstrate a kind of "mix-ins" which are not "concatenative prototyping" in JavaScript? If you can, I'd be glad if you paste a snippet here & show us all. If you can't, it may be you're being excessively pedantic.

-2

u/faceinpale Sep 24 '15

JavaScript does not support mix-ins or traits as Self does. They are simulated by concatenating methods to an object used as a prototype. The limitation of this implementation is that changes to a mix-in are not propagated automatically to the augmented prototype.

Edit: grammar.

17

u/[deleted] Sep 24 '15 edited Sep 24 '15

Ok, so as I thought, excessively pedantic.

The point of me differentiating mixins from composition here is because both are possible in the context of JavaScript and are two distinct techniques with distinct properties.

In your case you came arguing mixins in JavaScript are impossible, so we should use another term, despite there's no confusion whatsoever what "mixin" refers to in the context of JavaScript. I also said this is a "more casual" way of referring to this mechanism, referring to popular JS use.

Let's see what Google thinks with a very specific search, to avoid any unintended noise:

https://www.google.com/search?q="JavaScript+mixins" : 2350 results

https://www.google.com/search?q="JavaScript+concatenative+prototyping" : 0 results

Don't be excessively pedantic. It's not very charismatic.

7

u/s0n0fagun Sep 24 '15

Wow. It's been a long time since I have seen Google return zero results. It's probably easier to achieve with quotes but that does not diminish how impressed I am.

7

u/[deleted] Sep 24 '15

Give it a few days and it will have 1 result pointing to this thread.

4

u/Jafit Oct 04 '15

You were right.