Comparison
Don't repeat yourselfvsRule of three
Don't repeat yourself
the VAT rate changed and you had to find it in eleven places, and missed one.
Every piece of knowledge should have one authoritative representation. The original framing is about knowledge, not text: two identical-looking blocks that encode different decisions are not a violation, and two very different-looking pieces of code that encode the same rule are. Misapplied at the level of characters it produces the worst abstractions in most codebases, which is why the counter-slogans exist.
Full entry →Rule of three
you copied the block a second time and left it alone, and only pulled it out on the third.
Wait until you have three cases before abstracting over them. Two examples are not enough to see which parts genuinely vary, and an abstraction built on two is usually the wrong shape, which is worse than duplication because it must be unpicked before it can be fixed. It is the practical middle between religiously removing duplication and never noticing it.
Full entry →