Don't repeat yourself
||It has been suggested that this article be merged with Code reuse. (Discuss) Proposed since May 2016.|
The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system". The principle has been formulated by Andy Hunt and Dave Thomas in their book The Pragmatic Programmer. They apply it quite broadly to include "database schemas, test plans, the build system, even documentation." When the DRY principle is applied successfully, a modification of any single element of a system does not require a change in other logically unrelated elements. Additionally, elements that are logically related all change predictably and uniformly, and are thus kept in sync. Besides using methods and subroutines in their code, Thomas and Hunt rely on code generators, automatic build systems, and scripting languages to observe the DRY principle across layers.
DRY vs WET solutions
Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for either "write everything twice", "we enjoy typing" or "waste everyone's time". WET solutions are common in multi-tiered architectures where a developer may be tasked with, for example, adding a comment field on a form in a web application. The text string "comment" might be repeated in the label, the HTML tag, in a read function name, a private variable, database DDL, queries and so on. A DRY approach eliminates that redundancy by leveraging frameworks that reduce or eliminate all those edit tasks excepting the most important one, leaving the extensibility of adding new knowledge variables in one place. This is similar to refactoring in terms of removing duplications but differs in that DRY refers to extending knowledge capacity of a system where refactoring focuses on duplicate algorithms.
- Abstraction principle (programming)
- Code reuse
- Disk mirroring
- Redundancy (engineering)
- Rule of three (computer programming)
- Separation of concerns
- Single Source of Truth (SSOT/SPOT)
- Structured programming
- Dave Thomas, interviewed by Bill Venners (2003-10-10). "Orthogonality and the DRY Principle". Retrieved 2006-12-01.
- Justin Lee (2006-03-08). "DRY is for losers". Retrieved 2013-08-31.
- Alex Papadimoulis (2011-12-08). "The WET Cart". Retrieved 2012-05-21.
- Kevin Greer (2016-02-05). "FOAM DRY + WET". Retrieved 2016-03-09.