<story><title>Why composition is often better than inheritance</title><url>http://joostdevblog.blogspot.com/2014/07/why-composition-is-often-better-than.html</url></story><parent_chain></parent_chain><comment><author>al2o3cr</author><text>Meh. I think this phrase has been repeated until it has lost any connection with the original intent and turned into a generic &amp;quot;INHERITANCE BAD! COMPOSITION GOOD!&amp;quot; without much meaning attached to either word.&lt;p&gt;I haven&amp;#x27;t found the original source, but I&amp;#x27;ve always presumed the statement originally referred to some of the bizarro &amp;quot;inheritance-as-composition&amp;quot; stuff in the early C++ days: for instance, you might have a class &amp;#x27;Window&amp;#x27; and a class &amp;#x27;Button&amp;#x27;, then combine them with multiple inheritance to get a &amp;#x27;WindowWithButton&amp;#x27;, then inherit from &lt;i&gt;that&lt;/i&gt; and a &amp;#x27;Scrollbar&amp;#x27; class to get &amp;#x27;WindowWithButtonAndScrollbar&amp;#x27;.&lt;p&gt;I can&amp;#x27;t imagine anybody thinking of that as a &amp;quot;good&amp;quot; pattern today, but remember it was the &amp;#x27;90s. :)&lt;p&gt;Nowadays, the basic statement has been dogmatized to the point where you get code like this:&lt;p&gt;&lt;a href=&quot;https://github.com/elm-city-craftworks/broken_record/blob/master/lib/broken_record/composable.rb&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;elm-city-craftworks&amp;#x2F;broken_record&amp;#x2F;blob&amp;#x2F;ma...&lt;/a&gt;&lt;p&gt;This code re-implements Ruby&amp;#x27;s built-in method lookup algorithm, but with per-instance objects and none of the optimizations available to the real thing. It basically remakes inheritance, slowly and poorly, using composition.&lt;p&gt;The other one that makes me scratch my head: people who rail against inheritance, then suggest mixins as an alternative. At least in Ruby, the two are &lt;i&gt;equivalent&lt;/i&gt;. Check the `ancestors` property on a class with mixins sometime if you don&amp;#x27;t believe me.&lt;p&gt;TL;DR (too late) - use your damn brain to make decisions, not just parrot slogans.</text></comment>
<story><title>Why composition is often better than inheritance</title><url>http://joostdevblog.blogspot.com/2014/07/why-composition-is-often-better-than.html</url></story><parent_chain></parent_chain><comment><author>chton</author><text>While it&amp;#x27;s a well-written article, it really seems like beating a dead horse.  Composition over inheritance is a basic rule of OO programming, so much so that it has its own wikipedia page (&lt;a href=&quot;http://en.wikipedia.org/wiki/Composition_over_inheritance&quot; rel=&quot;nofollow&quot;&gt;http:&amp;#x2F;&amp;#x2F;en.wikipedia.org&amp;#x2F;wiki&amp;#x2F;Composition_over_inheritance&lt;/a&gt;)</text></comment>