DRY

expression

programming

Stands for: Don't Repeat Yourself

The principle that every piece of knowledge should have a single, authoritative representation in a system.

Coined in The Pragmatic Programmer, DRY says duplicated logic means duplicated bugs and drift, so you factor the shared idea into one place. Its counterpart is a caution - forcing unlike things to share code just because they look similar is its own trap - so DRY is about knowledge, not mere text.

Don't repeat yourself is from The Pragmatic Programmer, and the original wording is more careful than the acronym suggests: every piece of knowledge should have a single, unambiguous, authoritative representation within a system. The subject is knowledge, not text, and that distinction is where most misapplication starts.

Two pieces of code that look identical but encode different decisions are not duplication, and merging them creates a coupling that will hurt the moment those decisions diverge. This is the failure mode DRY produces in practice: an abstraction extracted from superficial similarity, then contorted with flags and special cases as the callers drift apart, until it is harder to read than the duplication it replaced.

The counterweight worth knowing is the rule of three, and the observation that a little duplication is far cheaper than the wrong abstraction. Duplication is visible and easy to fix later; a bad abstraction is load-bearing and hard to remove. The practical discipline is to wait until the pattern is clear, ask whether the repeated thing represents the same decision rather than the same characters, and prefer duplicating until the answer is genuinely yes.

Also known as: Don't Repeat Yourself, DRY principle

Sources

  • Hunt & Thomas, 'The Pragmatic Programmer' (1999)

All glossary entries