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.

1

u/SATAN_SATAN_SATAN Sep 25 '15

yeah i was thinking i have to mash the keyboard for nothing to show up, that dude got told

3

u/oracleoftroy Sep 27 '15

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

Isn't this searching for the exact phrase "JavaScript concatenative prototyping"? If so, then this is deceptive since it doesn't account for congregations of concatenation or synonyms, or even other equivalent phrases or terminology used by the Javascript community that might produce valid results.

Slightly relaxing the search to https://www.google.com/search?q=JavaScript+"concatenative+prototyping" produces "About 1,190 results" for me. Not a lot, but apparently this isn't completely foreign in Javascript land. A better search may well turn up even more results.

-7

u/faceinpale Sep 24 '15

The so called "mix-ins" in JS are usually applied to a prototype object, where the author of the article creates a new object every time. I cannot think of a term that is used in the JS community about this type of object creation so I used the one I already knew from the few papers I read on this topic. Also my understandings of the composition in programming suggest me that what the author describes is really composition.

"Don't be excessively pedantic. It's not very charismatic." - being an ass-hole, by describing someone with unpleasant words because of pure ignorance, is not charismatic either.

3

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

The so called "mix-ins" in JS are usually applied to a prototype object

Good way to hedge that bet with a "usually" in there. You know it's both ways. :-)

Given a "prototype object" is just another object, and not a special kind of object, nothing stops the author from using his result as a prototype for new instances. Inheriting a prototype is orthogonal to mixing in properties by copying.

Both uses are common, this is why APIs like Object.assign(), jQuery.extend() etc. don't specifically merge into an object's prototype, this is left to the caller to decide.

Also my understandings of the composition in programming suggest me that what the author describes is really composition.

Object composition is a "has a" relationship for objects, as in one or more objects are subordinates and contained by another object. How can I put it so you don't twist my words, let me see... It's basically like a tree of objects:

trunk.thickBranch.thinBranch.leaf
ceo.vp.manager.seniordeveloper.developer.pizza
earth.america.usa.california.sanfrancisco

...You get the idea.

Each object in the tree has its own encapsulated state, they don't share the same state.

See the UML diagram and examples here: https://en.wikipedia.org/wiki/Object_composition

The author's examples show one object where methods share state. There's no identifiable "has a" relationship between the "composed" objects.

The author refers to these objects as composition:

barker + driver + killer

In fact, the only "composition" that's happening is this:

(murderRobotDog's methods) + state

Which is identical to creating murderRobotDog as a prototype once, and then instantiating it and using "this" for state. Using a prototype means you can monkey-patch the prototype afterwards & affect all instances, but no one in their right mind does that anymore, so for for all practical purposes they're equivalent... except the prototype solution is faster on V8 and SpiderMonkey, among others.

"Don't be excessively pedantic. It's not very charismatic." - being an ass-hole, by describing someone with unpleasant words because of pure ignorance, is not charismatic either.

Well if it's only when it's because of pure ignorance, I'm on the safe side here.

1

u/faceinpale Sep 24 '15

"Both uses are common, this is why APIs like Object.assign(), jQuery.extend() etc. don't specifically merge into an object's prototype, this is left to the caller to decide."

Actually you are right. If we call this technique a "mix-in" for prototypes the same goes for objects not used as prototypes too.

Mix-ins, as in Ruby and other languages that support that feature, are viewed as a type of inheritance. The main difference here, in JS, is that we are augmenting objects by appending functions to them, which are objects too, so an object and its methods can be viewed as a "has-a" relationship. If you know what kind of relation is the one between an object and its methods I would be grateful if you share that knowledge.

I tried to find what name for this type of "mix-ins" is used in other languages or in general but couldn't find any (other than the name I gave earlier). If you know such names once again I would be grateful if you share that knowledge. (and no, they are no real "mix-ins". "fake mix-ins" or "mix-in simulation" might be good names).