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.

  • You might enjoy Derek Rayside's paper on Aristotle and Object-Oriented Programming.

  • "... In reality, you only inherit half your genetic makeup from each parent, ..."

    Not if you are a bacteria.

    Perhaps we should downgrade the metaphor.

  • Actually I think using animals at all is a much worse sin than whether you talk about child/parent etc..

    Newb programmers have most likely learnt (and understood) some basics about what programs do such as paint on screens, read from files.

    Then "trainers" and OO intro tutorials start telling tham about animals and shapes.. and their confidence is destroyed becuase they haven't a clue what on earth the point of an animal class is in the context of code!!

    Worse still the same trainers use large unhelpful headings such as Polymorhism, Encapsulation and yes I agree Eric even the term inheritence isn't intuitive enough, at least though it's a term they have heard of before and it's that scary!

    Time and time again new programmers at my company who have done a computer science degree just don't really get OO becuase everything they were taught was so abstract.

    i use a file importer program to demo OO and it makes sense to them.

    it has a concrete real life feel to it. It's a complex enough problem to justify OO but not to complex that it blows their mind.

  • Thought provoking article as always, Eric.   Great comments, also.

    I agree with Mike G. - I don't think the animal metaphor is very good for beginners: I know it confused me in the beginning because it just did not seem to relate to programming.

    I don't mind the parent/child terminology, because to me it reflects the tree heirarchy well and the direction of the heirarchy.  For some reason, the terms "less derived" and "more derived' are just not intuitive to me and I end up translating them to "is a parent" and "is a child" in my mind.

    I don't agree with the rant about interfaces being implemented instead of inherited.  I like the word inherited for both the base class and the interfaces.  The child class is inheriting the method signatures for both base classes and interfaces and inherits the type of both the base classes and interfaces.

    The implementation to me is separate concept.  If the base is completely abastract and virtual the the child class must implement all the methods.  If a class already has all the methods in an interface then it does not have to implement anything.

  • I think it is not the case, that you can't map mammal as parent of giraffe. Real problem is that newbs can't map abstract things which are present in programming environment to such relations as in animal kingdom. Ok, mammal is quite abstract, but we have some intuition about those "animal" things. For those programming things we do not have any intuition at the beginning because there are much more abstract things to grasp, so we should learn by those totally abstract concepts which are present in real software.

  • Eric,

    Your latest blog posts (and possibly earlier too, haven't read enough) could easily make a beautiful book on software. Have you considered making them a book?

  • Did you see INCEPTION before explaining inheritence.... you shouldn't. Take an orange juice and write this article again.

  • What I like about the animal metaphor is that it is a hierarchy that everybody can instantly understand. If one is trying to come up with an example to relate to a situation, it is trivial to come up with more examples (two classes derived from the same base -- giraffe and zebra are both mammals; two classes derived from the same base but with different intermediate derivations -- cat and shark are both animals, but cat is a mammal and shark is a fish).

    If animals aren't a good metaphor, I'd like to know what to use instead.

  • @Gabe: I think the point is not to use an abstract metaphor at all but to use actual real world code examples instead.  A few commentors gave examples:  file importing and graphical example.

  • @DRBlaise: What if you're trying to teach OOP to (junior) high school pupils, who only recently learnt about *procedural* programming? I am the only guy in my class who knows something about programming, about it works, etc. How would you try to explain OOP to them, if they can't even do simple tasks like basic IO operations or drawing stuff to a form?

    This will never happen though, we're learning VBA and there's nothing about classes (or modules) in our book, sigh...

  • At least part of the reason that the animal metaphor has problems is that the bilogical taxonomy is essentially in a state of being refactored from being based on behaviour and physical characteristics to genetics. Genetically it seems there is no such thing as a fish

  • @Les - two examples of OOP I would use to teach students, would be games and GUI.

    Designing or theorizing about an OOP game would be something students could really get into and there are all sorts of heirarchies and "is a" and "has a" relationships: characters, weapons, inventory, etc.  There could even be an animals in the game and you could talk about a heirarchy of animal behaviors in the game versus how real animals are categorized.

    GUI design is something they can see right in from of them and you could talk about "objects" on the screen:  forms, menus, borders, scroll bars, text boxes, drop downs, etc.

  • I love your footnotes.

    "Assuming that we're talking about members of a sexually reproducing species."

  • @JK The article says "Or put another way, if we go back to most recent common ancestor of everything we now call fish (including the incredibly primitive lungfish and hagfish), we find that they also were the ancestor of all four-legged land vertebrates, which obviously aren’t fish at all." This is news? Hasn't this been true of reptiles and birds for ages? Normal people don't reject paraphyletic groups.

  • While I am a technologist by trade, my biggest takeaway from this article is that I spent 11 minutes learning all about the Right Honorable Sir Nigel Tufnel.

Page 2 of 3 (37 items) 123