Table of Contents
Don't Repeat Yourself (DRY)
Variants and Alternative Names
- Single Point of Truth (SPOT)
- Single Source of Truth (SSOT)
“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”1)
DRY not only states that code duplication shall be avoided. Rather DRY is a general rule that states that if there is duplication, there shall be some “single source of truth”. Also when one piece of information has several representations (like an object structure corresponding to a database schema) DRY demands one and only one representation being the definitive one. The other representations have to be generated automatically. The “one and only” representation can be one of the used representations or alternatively a third one.
If there are several representations of the same information (be it code or any other form of information), all of them have to be maintained separately while changing at the same time. There is the danger that at some point in time the different representations diverge which is a fault. But if there is a single source of truth, there is only one place where changes have to be applied. Then the representations cannot diverge.
- Add a new invokable module (a function, a method, a class, etc.) instead of duplicating code
- Factor out a common base class
- Use code generation when information has to be represented in multiple forms
- Use polymorphism to avoid repeatedly enumerating a set of possible solutions in if or switch statements
See section contrary principles.
Andrew Hunt and David Thomas: The Pragmatic Programmer: From Journeyman to Master
- Examined: There is extensive research on code cloning, its reasons, automatic detection of code clones, their evolution, etc. In 2) Chanchal Kumar Roy and James R. Cordy present a 115 page survey on the state of research as of 2007. Many unanswered questions remain and research is still ongoing. In 2009 Juergens et al. 3) analyzed fife systems written in C#, Cobol and Java each between around 200 and 500 kLOC. They identified clones and intentionally and unintentionally inconsistent changes to them. They then related the faulty clones to them and came to the expected conclusion that clones are changed inconsistently and that this results in faults. So at least the part of DRY about code duplication is supported by research findings.
- Accepted: It is generally agreed upon that code duplication is to be avoided. On the other hand the broader meaning of DRY which results in the heavy use of code generators is often not considered. On the other hand The Pragmatic Programmer is a well known book which makes DRY a well-known and accepted principle.
Relations to Other Principles
- Murphy's Law (ML):Duplication is a typical example for error possibilities. In case of a change, all instances of a duplicated piece of information have to be changed accordingly. So there is always the possibility to forget to change one of the duplicates. DRY is the application of ML to duplication.
- Once And Only Once (OAOO): This is the aspect of DRY which is concerned with the avoidance of (code) duplication.
- Single Choice Principle (SCP): Duplication can also be duplication of information about a set of possibilities. The SCP is about this aspect.
- Write Code That Writes Code: This is the code generation aspect of DRY.
- Keep It Simple Stupid (KISS): Especially code generators can be very complex.
- Generalization Principle (GP): A generalized solution avoids duplication.
Discuss this wiki article and the principle on the corresponding talk page.