Bad Metaphors

Bad Metaphors

Rate This
  • Comments 37

The standard way to teach beginner OO programmers about classes is to make a metaphor to the real world. And indeed, I do this all the time in this blog, usually to the animal kingdom. A "class" in real life codifies a commonality amongst a certain set of objects: mammals, for example, have many things in common; they have backbones, can grow hair, can make their own heat, and so on. A class in a programming language does the same thing: codifies a commonality amongst a certain set of objects via the mechanism of inheritance. Inheritance ensures commonalities because, as we've already discussed, "inheritance" by definition means "all (*) the members of the base type are also members of the derived type".

Inheritance relationships amongst classes (**) are usually designed to model "is a special kind of" relationships. A giraffe is a special kind of mammal, so the class Giraffe inherits from the class Mammal, which in turn inherits from Animal, which inherits from Object. And that's great; this clearly represents "is a special kind of" relationships. I have always, however, had a problem with the fundamental metaphor of "inheritance". Why "inheritance"? You inherit genetic information, property, and if you're a titular lord, your peerage, from your parents. And if you make a diagram of a class hierarchy, it looks a bit like a "family tree" in which the derived class is the "child" of the base "parent" class. And indeed, people often speak of the base class as the "parent" class of a "child" derived class, particularly when speaking to beginners.

But the "parent-to-child inheritance" metaphor is awful. A giraffe is not "a child of mammal"; a giraffe is a child of Mr. and Mrs. Giraffe. A "child" is not "a special kind of parent". In reality, you only inherit half your genetic makeup from each parent, and you can inherit real property from any relation, or for that matter, from any non-relation. In programming languages you only "inherit" from related types, and you inherit all their members (*). In reality, everyone has two parents (***), but in programming languages some languages allow inheritance from arbitrarily many "parents", some allow exactly one. In reality, a single, specific person inherits specific property from a single, specific parent, and two different children can have entirely different inheritances from their parent; in programming languages, the "inheritance" relationship does not apply to individual objects, and every child inherits exactly the same thing from the parents. And in reality you only inherit real property when the decedent is dead!

But wait, it gets worse. The parent-child metaphor is ambiguous in any language that supports both lexical nesting and nominal subtyping of classes:

class B<T>
    class D<U> : B<U> { }

Quick, what type is the "parent" of type B<string>.D<int>? Is it B<T> or B<string> or B<int>? That type is lexically inside B<T>, logically inside of B<string>, and derived from B<int>; which of those three is its parent? If you drew a graph showing either lexical or logical containment relationships, it would form a graph that looks every bit as much like a "family tree" as the graph showing inheritance relationships. And lexical containment allows access to all the properties of the container from the contained type, even including not-inherited and normally inaccesible members like private constructors! It is not at all clear that one kind of "parentage" is actually more "parent-like" than any other.

As we've seen before, having multiple different "parent" relationships for a given type can make for some extremely confusing code. We have to be extraordinarily careful when writing the specification and the compiler to ensure that we unambiguously describe precisely the relationship we wish to describe. I therefore try hard to avoid "parent-child" metaphors entirely; it is much more clear when writing an example to describe the type relationship as "base type and derived type", rather than "parent type and child type".

(*) Excepting constructors and destructors.

(**) I'm going to stick to talking about class-based inheritance here; my criticisms apply equally well to interface-based inheritance but I don't want to open the can of worms that is all the subtle differences between class and interface inheritance. And I've never much liked the inheritance metaphor on interfaces anyways; a "contractual obligation" metaphor is better.

(***) Assuming that we're talking about members of a sexually reproducing species.

  • Typo?

    "all (*) the members of the base type are also members of the derived type"

    A giraffe (derived) is a mammal (base), but a mammel is not (necessarily) a giraffe.

  • Re: you ** comment on inheritance from interfaces, this is a pet hate of mine, a class implements the interface, it does not inherit from it. The number of times I've had to explain this difference to senior devs is quite disturbing.

  • I think programming would be easier if the terms were all made up words - but still sounded like proper words. Having learned programming very early before I knew most of the more advanced English words, to me it seems inheritance is the perfect word for base/derived class relationships, but no so fitting for real life usage for exactly these reasons. Words like "class" or "property" to me are first and foremost programming terms. Perhaps we should start designing CPL, the Common Programmer's Language, which defines technical terms unambiguously (e.g. doesn't have the word 'dynamic'), and has a term for every single concept used by any programmer ever, so that we would be able to actually speak a language without confusing metaphors or ambiguities? Nah... Nobody would use it.

  • Actually the term 'inheritance' makes good sense, only it applies to types and not to objects! The derived <i>type</i> B inherits all members (*) from its base type(s) A<sub>i</sub> just as children inherit their properties from their parents, and each inherited member is inherited from exactly one parent (except weird cases like virtual inheritance, where the member is inherited from one grandparent through one or more parents). Thus IMO the ultimate cause of problems in this particular case is confusion about the type-object distinction.

  • @Kyle members meaning methods, properties, etc.  You are confusing with *instances* of the types.

  • @Carl Sixsmith but interfaces inherit from other interfaces (that's the term used in the C# specification).

  • While this is somewhat orthogonal to your discussion, I've always loved this essay by Kragen, which shows exactly how terrible "Giraffe extends Animal" can be.

  • "Quick, what type is the "parent" of type B<string>.D<int>"

    I don't know.  Isn't that a good reason not to use this kind of design?  Even with really good names for B and D, I think it would be difficult for me to grasp what this relationship defines.  Sort of like your "Smart Pointers Are Too Smart" example.  


  • Usage of mammals in the example may the problem here. Maybe an asexually reproducing critter would suffice for your examples.

  • @phoog: Yes, that makes more sense then.

  • @Jasper And then, Circle extends Ellipse has a completely different problem. (Less so for immutable types, but still a problem)

  • Perhaps "parent" and "child" don't refer directly to family relationships, but are themselves computer science jargon for nodes in a tree structure. So the parent class is adjacent to the child class in the class heirarchy (tree) but it is the one closer to the root of the tree. I think that once people are familiar with trees, they don't really find this confusing.

  • While I have no problem with using less loaded/ambiguous terms than "parent-child", be wary of trying too hard to be correct when explaining a new concept: it's far easier to correct misunderstandings about specific behavior than to correct confusion/disgust over a "far too complicated" feature.

    When first introducing OO (really class-based, but lets not go *there*!), the essential detail that you *need* to get across, ignoring everything about members, behavior, etc..., is the "is-a" relation. Using real-world examples that the student is immediately going to map to such relations, despite the fact that they don't actual match Liskov or would be data-driven in a real implementation, is far more helpful than dryly describing the properties of "is-a". When the smart student then says later "A-ha! But penguins can't fly!", the correct response is "the real world is complicated", or "So it's probably a bad idea to actually have a 'Bird' base class", not to try to re-explain OO from scratch again!

  • @Carl: Agreed. I've seen even teachers don't understand "interface" is just a "contract" declaring what "services" you need to implement and nothing else.

    What's more? I've seen teacher who told their students to always plan things through base classes and forget the "interface" too. And people wonder why others think OOP is difficult!

    Of course that's 10+ years ago and let's hope things would be better now.

  • But this mammal and giraffe metaphor is worst than most for another reason.

    Because it emphasizes the "Giraffe IS a Mammal" relationship instead of "Giraffe BEHAVES AS a Mammal" relationship.

    This is important when you reach the classical corner cases. While in classic taxonomy it makes sense for whales being mammals, in an OOP world there is no advantage in having whales are mammals instead of fishes (besides, mammal is a completely meaningless category in OOP - unless you consider Sweat() a method you can call :)).

    Also, classifying objects for Behaviour has the advantage of making structural patterns (adapter, composite and above all decorator) much easier to introduce.

Page 1 of 3 (37 items) 123