7

Today I answered this question:

Should I use strategy design pattern or something else?

After some back and forth with the asker in the comments, I thought it prudent to bring up the point that composition would be better than inheritance in his design.

I searched the main site looking for a good question on the topic of "composition over inheritance" but could not find a good one with lots of useful information, examples, etc. that demonstrate the strengths and weaknesses and makes a convincing argument in favor of composition. I ended up linking to a good enough question but was not satisfied with it.

Is there a question on the main site that would be a good candidate as the canonical "composition over inheritance" question that would be a good question to which to link when writing answers that advocate for composition instead of inheritance?

If not, I will consider writing up a self-answered CW Q/A.

| |
  • 2
    I would also like to point out that there are plenty of answers to other questions with good information, but I could not find a good question on this topic. Ideally, all of the relevant information would be contained in a single canonical question, not strewn across multiple other, unrelated questions. – user22815 Oct 10 '15 at 20:33
  • Normally when someone asks a question about "Why is X better than Y?" or "Why does everyone say X is better than Y?" we close it as opinion, too broad or unclear. So it's entirely possible we had a question kind of like this and eventually closed and/or deleted it. – Ixrec Oct 10 '15 at 20:57
  • 1
    @Ixrec I think this is one of the exceptions to the rule. While certainly there is some opinion here, it is not primarily opinion-based because there is broad consensus on the topic. But yes, it is entirely possible that such a question is deleted. – user22815 Oct 10 '15 at 21:00
  • Yeah, I'm sure with you writing it (and declaring it as canonical) it'd avoid that fate and hopefully be of some use. – Ixrec Oct 10 '15 at 21:03
  • @Ixrec this topic comes up frequently enough when answering design questions that it is worth having a reference to point to. I just don't want to write up a dupe. – user22815 Oct 10 '15 at 21:04
  • two of them are listed here – gnat Oct 11 '15 at 6:05
  • @gnat I don't think I am convinced of the quality of the questions or their answers. There is some good information in there, but it is a bit sparse. – user22815 Oct 11 '15 at 16:06
  • well first question is more about history, a convenient target when people ask where this came from. Top voted answer in second question looks fairly solid to me. Granted it's large and not very decisive but that's because the topic itself is such. If you want something more instructive, consider this answer in one of the dupes - per my reading, answerer managed to make it concise without too much compromising on accuracy – gnat Oct 11 '15 at 19:26
  • 1
    I know my design patterns book says 'prefer composition over inheritance'. The one answer I've read which I like and you may want to reference if you do write one up: programmers.stackexchange.com/questions/282024/… – Luminous Oct 20 '15 at 19:28
  • 1
    While not the most ideal of names, consider Where does this concept of “favor composition over inheritance” come from? ? – user40980 Oct 20 '15 at 21:32
4

Self-answered/Canonical questions get a bit of a free pass on Stack Overflow, if the question/answer pair is good. In particular, if the answer makes a really good target for questions that are repeatedly asked often, it often gets a pass, and deservedly so.

For good examples of these kinds of questions, see:

and

...both of which are referenced in the PHP Tag Wiki.

The PHP Tag Wiki, by the way, is the right way to create a tag wiki. It is a shining example of what Tag Wikis should look like.

Some more examples of excellent canonical question/answer pairs:

Some questions just get lucky:

Anyway, the way you find these questions is by looking through the Tag Wikis. All of the decent tag wikis have a list of Frequently Asked Questions that the tag community sees as worthy enough to reference in the tag wiki.

By the way...

| |

You must log in to answer this question.