from Hacker News

Why composition is often better than inheritance

by exch on 7/6/14, 11:36 AM with 90 comments

  • by al2o3cr on 7/6/14, 5:11 PM

    Meh. I think this phrase has been repeated until it has lost any connection with the original intent and turned into a generic "INHERITANCE BAD! COMPOSITION GOOD!" without much meaning attached to either word.

    I haven't found the original source, but I've always presumed the statement originally referred to some of the bizarro "inheritance-as-composition" stuff in the early C++ days: for instance, you might have a class 'Window' and a class 'Button', then combine them with multiple inheritance to get a 'WindowWithButton', then inherit from that and a 'Scrollbar' class to get 'WindowWithButtonAndScrollbar'.

    I can't imagine anybody thinking of that as a "good" pattern today, but remember it was the '90s. :)

    Nowadays, the basic statement has been dogmatized to the point where you get code like this:

    https://github.com/elm-city-craftworks/broken_record/blob/ma...

    This code re-implements Ruby'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.

    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 equivalent. Check the `ancestors` property on a class with mixins sometime if you don't believe me.

    TL;DR (too late) - use your damn brain to make decisions, not just parrot slogans.

  • by userbinator on 7/6/14, 2:06 PM

    While it's possible to overuse inheritance, I don't think replacing it with composition is all that much better, and in addition all those forwarding methods that do nothing more than call another (could they even be optimised out?) are a great example of code that would need to be written, consuming resources like programmer time, but otherwise serves no true useful purpose to the functionality of the software. The complexity only changes form, so instead of tracing the flow through an inheritance hierarchy you're just doing it through chains of forwarding methods. It's for this same reason I don't believe so much his argument for readability and short classes - breaking everything up does not make things simpler, it makes the complexity spread out over a larger area; while it may be true that it is easier to understand an individual piece, it becomes more difficult to understand the system as a whole. This is especially important when debugging, where "can't see the forest for the trees" is a big hindrance.

    I think his example of flexibility is the strongest argument for composition, because in that case the forwarding methods are not a waste - they would need to do (useful) work to determine which of the multiple composited objects they would need to work with.

    Being mostly a C programmer who does OO-things, I use inheritance when it's obvious that most of the "methods" will be passthroughs to the "superclass", and composition when there is something more that needs to be done. Also, as I am not constrained by the OO model/conventions of the language, it's more flexible in that I can do things like "inherit" multiple times and even change that at runtime, so there is really no strict separation between composition and inheritance; to me, it's just "which function do I set this to point to."

  • by chton on 7/6/14, 12:46 PM

    While it'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 (http://en.wikipedia.org/wiki/Composition_over_inheritance)
  • by kissgyorgy on 7/6/14, 1:43 PM

    In Python, we use mixins. Mixins can only inherit from 'object' and nothing else, like this:

        class PhysicsobjectMixin(object):
            def update_physics(self):
                pass
            
            def apply_konckback(self, force):
                pass
    
            def get_position(self):
                pass
    
    
        class FightMixin(object):
            def attack(self):
                pass
    
            def defend(self):
                pass
    
        
        class TalkMixin(object):
            def say_something(self):
                pass
    
    
        class Character(PhysicsobjectMixin, FightMixin, TalkMixin):
            pass
    
    
        class Pickup(PhysicsobjectMixin):
            pass
    
    
        class Projectile(PhysicsobjectMixin):
            pass
    
    
    it's still inheritance, but the classes will be flat; every class only inherits one deep, so there will be no diamond problems and no repeating code.
  • by millstone on 7/6/14, 8:19 PM

    Consider how these things get stored. In the inheritance model, you might have a big quad tree or some other data structure of PhysicsObjects, and just run through and call updatePhysics() on all of them.

    In the composition model, we now have multiple classes (Character, Pickup, Projectile), each with an unrelated updatePhysics(). This means code duplication to call the relevant method on each separate class.

    We could relate them all via an interface, instead of inheritance; now we can store IEntity or whatever. We will soon discover three needs that are awkward to address:

    1. Whenever we want to add some new method (say `fall`), we must go back and implement it separately in each class.

    2. Different classes will want to share implementations. For example, both Characters and Pickups bounce on fall.

    3. Some classes will want to specialize an implementation to do more. Characters bounce on fall, but also take damage.

    In practice you may end up with both: an interface that your engine talks to, but also a common base class that provides sane defaults.

    So while interfaces allow uniform interactions with disparate classes, inheritance provides that and also the ability to share and specialize the implementations. So inheritance solves some problems that interfaces cannot.

    See also default methods in Java, which makes an interface more like a class, and implementing an interface more like inheritance. The documentation even says that a class that implements an interface inherits its default methods.

  • by jiaweihli on 7/6/14, 12:54 PM

    I think this is a tooling issue. People initially tend to favor inheritance because it looks cleaner than composition. Mixing a lot of unrelated code in the same class makes things hard to find. (which method applies to which composed object?)

    In languages that build in a concept of traits/mixins however, this isn't an issue.

  • by taeric on 7/6/14, 6:33 PM

    I would prefer this with "why a shallower abstraction pool is better than a deep one." I've seen some compositional concoctions that were just as terrible to deal with as inheritance based ones. I think I've even contributed/originated some.
  • by zak_mc_kracken on 7/6/14, 3:59 PM

    We've known this since at least 1994, when the GoF book [1] famously said:

    "Favor object composition over class inheritance"

    [1] http://www.amazon.com/Design-Patterns-Elements-Reusable-Obje...

  • by pllbnk on 7/6/14, 3:23 PM

    When I try to choose between the two, I often like to think if the object I try to inherit from is from the same domain/context and solves a related problem. In the example in the article a PhysicsObject solves the problem of calculating coordinates in space and from the beginning it was not designed as something to be used in the game by itself. While the character participates in the actual game and executes the game logic. The character does not 'inherit' from PhysicsObject, it merely knows that PhysicsObject represents it in the space.
  • by teamhappy on 7/6/14, 1:53 PM

    It's pretty much the same example I used here a couple of days ago: https://news.ycombinator.com/item?id=7976227

    Game development seems to be the poster child for composition over inheritance.

    Here's a lengthy article that explains it way better (IMHO): http://gameprogrammingpatterns.com/component.html

  • by kilemensi on 7/6/14, 3:36 PM

    I think one of the biggest reasons why most libraries/frameworks/apps/etc. use inheritance over composition is the easy with which the underlying languages allow the use of inheritance as opposed to composition. Most of these software writers know SOLID and other OO principles but when they're faced with the actual implementation, inheritance is just too damned easy to implement.
  • by andybak on 7/6/14, 12:58 PM

    The Wikipedia article chton mentions (https://en.wikipedia.org/wiki/Composition_over_inheritance) ends with the following when discussing the drawback of composition (boilerplate for forwarding methods): "This drawback can be avoided by using traits or mixins."

    Now this is where things get a little blurry for me.

    Mixins can help with the main drawback of Composition - but Mixins ARE inheritance - so isn't this a contradiction?

    If I use PhysicsObjectMixin in my CharacterComposition class then I have to inherit from it. So aren't we back with the perils of inheritance?

  • by kstenerud on 7/6/14, 2:56 PM

    In this case I'd argue that the roles are a bit messy and Character has too much knowledge. Character should not know that physics objects can be updated, and certainly shouldn't be calling updatePhysics. You could end up with an updated Character interacting with a Character whose physics state hasn't been updated yet.

    applyKnockback: Character -> Physics object -> Physics engine

    updatePhysics: Physics engine -> Physics object -> Character new position (x, y)

    updateCharacter: Character reacts to change

  • by yayitswei on 7/6/14, 2:10 PM

    By the way, I encourage everyone to try out their game, Awesomenauts. Think Super Smash Brothers meets Dota. It's a lot of fun to play!
  • by javinpaul on 7/6/14, 3:12 PM

    Couldn't agree more than this. I have also shared my 2 cents on Why composition is better than Inheritance for Java Programmers here http://javarevisited.blogspot.sg/2013/06/why-favor-compositi...
  • by _pmf_ on 7/6/14, 2:32 PM

    The burden of proof should fall onto the user of inheritance to justify his decision. Interfaces plug delegates is more tedious to implement, but greatly reduces the chances of an architecture turning into a complete train wreck.

    I often wonder why declarative delagating is not a first class concern in programming languages.

  • by yeureka on 7/6/14, 9:54 PM

    In my opinion the best arguments against inheritance were written by Richard Gabriel in his book Patterns of Software: http://dreamsongs.net/Files/PatternsOfSoftware.pdf
  • by known on 7/6/14, 4:03 PM

  • by fithisux on 7/6/14, 2:35 PM

    Why often and not always? Can you provide an exceptional case?