Composition Over Inheritance

If you think of classes/objects as being composite structures, with the components having names, don't those names act metaphorically? Thus satisfying Real Metaphors Are Promiscuously Mixedđ“…®?

content

Inheritance is out of fashion these days in object-oriented languages, where many march under the banner of “prefer composition to inheritance”. So the core relationship changes from `X is_a Y` to `X has_a Y`. You’re no longer using two names – `X` and `Y` – to refer to a single object or data structure. You’re using the names to refer to two objects, one of which is a part of the other. So an `Invoice` might *have* a list of goods, but it isn’t a *kind* of list of goods.

There’s a figure of speech, synecdoche , that fits this situation. Synecdoche is when a part of something is used to refer to the whole thing. Examples: * “I’ve got a new set of wheels” when referring to a new car, or * “We need some new blood in the organization,” or * “There are a lot of good brains at that university.”

Lakoff and Johnson» point out that a synecdoche does more than just point to something; it also signals which of the many associations you might have for the whole are relevant right now.

* For example, “**good brains**” signals why you’re talking about people at the university: the need for **intelligence** is our topic.

* “A new set of **wheels**” focuses on the associations of wheels, which for the classic American teenager includes greater freedom of movement, which stands in for greater **freedom** from parental and adult control.

* “New **blood** in the organization” alludes to blood as the **life force** – just as vampires replace stale blood from their last meal with vital new blood, organizations replace tired-out employees with fresh ones. (“Fresh meat” is perhaps the more telling metaphor for employees most of the time.)

I can see this working for composite structures in programs. For example, casually referring to an `Invoice` object as the "item list" might help focus conversation between two people who are pair-programming. If you’re writing some routine that is only about the list-like aspects of an `Invoice` object, maybe naming the variable that points to it `item_list` might help the reader of the code, though it creeps me out, personally.

Usually, though, I think most programmers would think something’s off if they have to know about a data structure’s innards to use it.