Composition over Inheritance and other Pithy Catch Phrases

Love them or hate them, the ALT.NET mailing list is a source of interesting debate, commentary and insight. I can’t help myself but to participate. Debate is good. Stifling debate is bad. Period. End of debate. (see!? That was bad!)

The community itself is a young community, and as such, they are going through a period of identity forming. What are their shared values? What does it mean to be an ALT.NET-er? It's not exactly clear yet, but it is starting to form.

One thing I would caution this community is to be careful in how they define their shared principles. For example, in one thread one individual mentioned debating me and then in the same message proposed the idea of Composition over Inheritance as a shared principle.

In response, someone posted this:

You can throw the book at those people--literally. Favoring composition over inheritance is straight out of the Gang of Four book. Don't like design patterns? Fine. No problem. I have a couple of
Don Box COM+ books that say the exact same thing.

Here was my response, which I also wanted to put in a blog post since it represents pretty well what I think.

I think ALT.NET should focus more on the principles of thinking for yourself and a desire to improve.

> Favoring composition over inheritance is straight out of the Gang of Four book.

So is the Singleton pattern. So is the Template Method pattern.

Sorry, Appeal to Authority doesn't work for me. Look, I’m not against composition over inheritance in many cases. Perhaps most cases. What I am against is saying that it applies in all cases and that if you don’t do it, you’re not ALT.NET.

I’m against the blind application of these pithy catch phrases. Blindly applying a “best practice” is just as irresponsible as never applying a “best practice”. There is no perfect design. There is no one true way. There is no one size fits all.

Why favor composition over inheritance? What trade-offs are you making when you do so? Developers should think through these things when they make these choices. And when a developer does think through the issue, but makes a choice that differs from what you think, you should applaud that. At least the developer thought through the decision.

I don’t care that a developer doesn’t favor composition over inheritance in a specific case. I only care that the developer thought it through, had a reason for the decision, wants to improve.

The goal is not to bend developers to the will of some specific patterns, but to get them to think about their work and what they are doing. For example, one advantage with inheritance is that it is easier to use than composition. However, that ease of use comes at the cost that it is harder to reuse because the subclass is tied to the parent class.

One advantage of composition is that it is more flexible because behavior can be swapped at runtime. One disadvantage of composition is that the behavior of the system may be harder to understand just by looking at the source. These are all factors one should think about when applying composition over inheritance.

So while I agree that you should favor composition over inheritance, inheritance is still necessary. After all, “the set of components is never quite rich enough in practice.”

That quote is from Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides in Design Patterns. But don’t believe it just because they said it. After all, I would hate to be guilty of an Appeal to Authority. ;)

What others have said

Requesting Gravatar... Julian Birch Dec 11, 2007 2:42 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
Although I agree with everything you say here, I still think it's the single best piece of advice in the whole GoF book. Ironically, it's in the introductory chapter, and I've met plenty of developers who skipped the introduction...
Requesting Gravatar... Gökhan Dec 11, 2007 3:07 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
Someone on the very same list once said

"Design without patterns is the slowest route to victory. Patterns without design is the noise before defeat."

Original quote was from Sun Tzu, I believe.
Requesting Gravatar... Damien Guard Dec 11, 2007 3:19 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
I wish .NET better supported composition by letting you wire up your interface declaration to a private instance variable for all non-defined methods/properties of that interface.

I wrote a post about it (right at the end).

The jist of the syntax would be something like:


public class CustomerPriceList : IList<ProductPrice> goto productPrice
{
private IList<ProductPrice> productPrice = new IList<ProductPrice)();
public Customer Customer;
}


[)amien
Requesting Gravatar... John Chapman Dec 11, 2007 4:53 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
I couldn't agree with more with this. I feel that often times people back themselves in to a corner. I have heard others bring this same mantra of "composition is better than inheritance." While it is true that you could typically use one approach against the other, they really best serve different purposes. As a developer we need to be able to find the best tool for a given job. It's the classic saying of "when your only tool is a hammer every problem starts to look like a nail."
Requesting Gravatar... +dotNetTemplar+ Dec 11, 2007 6:56 AM
# Favor Thoughtful Adherence Over Blind Adherence
Requesting Gravatar... J. Ambrose Little Dec 11, 2007 6:57 AM
# Favor Thoughtful Adherence Over Blind Adherence
Far be it from me to put words in Phil's mouth, but I hope that folks recognize that his post about favoring
Requesting Gravatar... Dave Spake Dec 11, 2007 7:23 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
Funnily enough, the GoF book agrees with you. Part of their definition of a design pattern includes Consequence, which they define as the various trade-offs involved in applying that pattern, all of which should be weighed before it is applied. Taking a mantra like "favour composition over inheritance" out of context and using that as an unbending rule not only goes against that philosophy but highlights the dangers of reducing sound advice to the level of soundbites.
Requesting Gravatar... Jason Haley Dec 11, 2007 7:25 AM
# Interesting Finds: December 11, 2006
Requesting Gravatar... Larry Clapp Dec 11, 2007 7:36 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
You wrote your rant as if "favor" means "always use". It doesn't, and I don't think that most people think it does. I can *always* favor composition over inheritance, but that doesn't mean I will always *use* composition instead of inheritance. I dunno, I suppose that's the entire point of this entry, but to me it seemed like you are arguing over a straw man.
Requesting Gravatar... Justin Dec 11, 2007 7:38 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
While it is true that most of our community once they have learnt a pattern, regard every problem as a nail to be pounded with their newly crafted hammer, this particular advice is one of the best counterpoints to the legion vast who have yet to realize that inheritance implies an 'is a' relationship rather than a simple way to reuse the code of the superclass.

Likewise, there is nothing inherently wrong with the singleton pattern, nor the template pattern, each has its merits and it is the inappropriate application of said patterns that cause such great consternation.

There are far too many absolutes in our community and not nearly enough rational advice, this piece of advise is not one of them, it just needs to be explained in terms of a 'is a' and 'has a', and it makes much more sense.

In any case, describing a sentence prefixed with the word 'prefer' as 'pithy' seems out of place.
Requesting Gravatar... ASPInsiders Dec 11, 2007 7:58 AM
# Favor Thoughtful Adherence Over Blind Adherence
Far be it from me to put words in Phil's mouth, but I hope that folks recognize that his post about favoring
Requesting Gravatar... Haacked Dec 11, 2007 8:38 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
@Larry Yeah, I should have pointed out that in various places, when I mention that subclassing is a valid method in some particular case, people have thrown "Composition over Inheritance" at me as an almost automatic response, leaving out the favor part, which I believe is the key word. I changed the title of the post to reflect that.

As I said, I believe that is good advice. I really do. But to me, following that advice is less important than understanding why it exists. Once you understand it, then I believe in the right cases, you'll make a good choice.

@Justin I didn't mean to insinuate that Singleton nor Template Method are universally bad patterns. Actually, I like the Template Method pattern. It's a counterpoint to composition because it requires inheritance. It's an example of usability in API design.
Requesting Gravatar... Troy DeMonbreun Dec 11, 2007 8:50 AM
# re: Favor Composition over Inheritance and other Pithy Catch Phrases
Larry makes a good point, the definition of the word "favor" seems glossed-over. But, maybe Phil is vehement about not even favoring a practice because it is still a rule, even if not black and white.

As an aside, ALT.NET (the label and the movement) makes me think of the adage "nonconformity _is_ conformity".

The thing I DO like about ALT.NET is their tendency to, as Phil wishes above that it was their only tendency, "... focus more on the principles of thinking for yourself and a desire to improve."
Requesting Gravatar... Haacked Dec 11, 2007 9:39 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
@Troy - I cleared that up in my comment to Larry.

I wouldn't say I wish it was their only tendency. J. Ambrose's post in response to mind understood my point and was a thoughtful response. You can read my comment over there in which I clarified a bit of what I'm thinking.
Requesting Gravatar... Brennan Stehling Dec 11, 2007 1:53 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
Sometimes you cannot use inheritance to address your current task. I use inheritance and composition all the time and it comes down to whether or not I can delegate functionality and if inheritance even makes sense. My favorite example of composition is the GridView which extends the CompositeDataBoundControl. It is made up of Buttons, TextBoxes, Labels and all kinds of other controls. It does not inherit them, but instead leverages the properties and events on the child controls to orchestration the actions that are taking place. Meanwhile it can be useful to extend the TextBox control to add new functionality or to override the rendering. I like having both approaches available to me.
Requesting Gravatar... Sean Chambers Dec 11, 2007 3:24 PM
# What really are the ALT.NET Shared Values?
Phil Haack just wrote a post about Composition over Inheritance and how adopting practices just because
Requesting Gravatar... Ryan Ginstrom Dec 11, 2007 4:09 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
Well, the rule is "favor composition over inheritance," not "blindly apply composition regardless of other factors." As such, I think it's great advice. Maybe not so great as a core value, but definitely advice to heed.
Requesting Gravatar... Haacked Dec 11, 2007 4:31 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
@Ryan - I agree. I think what I didn't make clear is that it's been thrown at me as "Composition over Inheritance". I'll talk about a case where I believe inheritance is valid and get the response "Composition over Inheritance". Over and over again like a broken record.

Gread advice. Certainly. Better advice when you understand why it's great advice.
Requesting Gravatar... Ryan Ginstrom Dec 11, 2007 6:14 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
@Haaked - Definitely in agreement. Blindly applying this "rule" without understanding it is a bad thing.

Most design rules exist in a state of tension with other rules, the idea being to achieve equilibrium. In the case of "favor composition over inheritance," I think that one of the tensions is with "do the simplest thing TCPW."* Hopefully, the equilibrium point between them is a simple yet extensible system.

* True for statically typed languages at least
Requesting Gravatar... Christopher Steen Dec 11, 2007 10:21 PM
# Link Listing - December 11, 2007
Link Listing - December 11, 2007
Requesting Gravatar... Colin Jack Dec 12, 2007 4:49 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
Good post and yeah completely agree.
Requesting Gravatar... Sam Gentile Dec 12, 2007 5:54 AM
# New and Notable 210
Prodded by Mike to pick up the slack, I am trying to get one of these out each morning before all my
Requesting Gravatar... Jason Haley Dec 12, 2007 7:13 AM
# Interesting Finds: December 11, 2007
Requesting Gravatar... DotNetKicks.com Dec 12, 2007 2:36 PM
# Composition vs Inheritance
You've been kicked (a good thing) - Trackback from DotNetKicks.com
Requesting Gravatar... Troy DeMonbreun Dec 12, 2007 2:45 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
@Phil - yeah, what you're seeing was a response that I had in started in my browser before yours had shown up, hence the "Re: Favor..." still in my comment subject even though you changed it before you posted.

Basically it was a blog post "race condition" concurrency issue. :-)

Regarding ALT.NET tendencies comment, apologies, I should have said "main" instead of "only", or omitted it altogether.
Requesting Gravatar... silky Dec 13, 2007 1:48 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
news at 11: programmers should think for themselves?

what on earth has our trade come to that posts/comments like this are required? it's sad.
Requesting Gravatar... Troy DeMonbreun Dec 13, 2007 8:01 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
@silky - sad, but true, and not only occurring in just our trade, but in much of society.

As in Crowd Psychology, Group Think and
Sheeple to name a few of the observations about society/human nature.
Requesting Gravatar... Haacked Dec 13, 2007 9:13 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
@silky I think we *all* need a gentle reminder from time to time. It's easy to get caught up in a movement, a fad, something new. You learn a cool new pattern and start looking for nails to hammer with it. I think it's a natural tendency.

One that needs to be squashed! ;)
Requesting Gravatar... Mark Leighton Fisher Dec 14, 2007 9:58 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
As someone who tries to be a Software Engineer, I'm reminded of the old engineer's phrase: "Good. Fast. Cheap. Pick at most two." Software Engineering is all about making engineering choices -- inheritance vs. composition, build vs. buy, Open vs. Closed Source, etc. Closing off your mind by always picking one alternative means closing off choices that might be better for your particular project (possibly your next project).
Requesting Gravatar... kNocki Sep 29, 2008 1:18 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
Whoa! (I know this is way late.) What the heck does "Favor" mean anyway?
Requesting Gravatar... rosyth Oct 09, 2008 5:02 AM
# re: Composition over Inheritance and other Pithy Catch Phrases
Brennan are you from rosyth did u teach P3-youthful? If u do i think u are Mr Joseph(I Am Kester)
Requesting Gravatar... David Rogers Mar 29, 2010 2:09 PM
# re: Composition over Inheritance and other Pithy Catch Phrases
(In a world where search engine ordering defines currency, this is a happening conversation.)

For most junior programmers, blindly adhering to these patterns is likely to produce better designs than ignorance of them. If a junior programmer is doing design work, handing them a list of these pithy phrases and an example or two to demonstrate them is going to be very helpful.

Teaching junior programmers standards, such as design patterns, is a necessary first step in properly educating them. The wisdom to question patterns and to decide when *not* to use them comes with experience. But note the emphasis here on learning them as quasi-absolutes, aka “Best Practices”.

Learning “what to think about” is not a necessary first step in learning to think, but I think a lot of time is wasted when such guidance is missing.

What do you have to say?

(will show your gravatar)
Please add 3 and 5 and type the answer here: