principles:don_t_repeat_yourself
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
principles:don_t_repeat_yourself [2012-11-26 20:33] – typo christian | principles:don_t_repeat_yourself [2021-10-18 21:27] – old revision restored (2015-06-26 15:45) christian | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Don't Repeat Yourself (DRY) ====== | ====== Don't Repeat Yourself (DRY) ====== | ||
- | ===== Alternative Names ===== | + | ===== Variants and Alternative Names ===== |
* Single Point of Truth (SPOT) | * Single Point of Truth (SPOT) | ||
Line 8: | Line 8: | ||
===== Context ===== | ===== Context ===== | ||
- | * [[contexts:Software | + | * [[contexts:Object-Oriented |
+ | * [[contexts: | ||
+ | ===== Principle Statement ===== | ||
+ | > "Every piece of knowledge must have a single, unambiguous, | ||
- | ===== Description ===== | ||
- | > "Every piece of knowledge must have a single, unambiguous, | + | ===== Description ===== |
DRY not only states that code duplication shall be avoided. Rather DRY is a general rule that states that if there is duplication, | DRY not only states that code duplication shall be avoided. Rather DRY is a general rule that states that if there is duplication, | ||
Line 23: | Line 25: | ||
- | ===== Origin | + | ===== Strategies |
- | Andrew Hunt and David Thomas: //The Pragmatic Programmer: From Journeyman | + | * 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 | ||
+ | ===== Caveats ===== | ||
+ | See section [[#contrary principles]]. | ||
+ | |||
+ | |||
+ | ===== Origin ===== | ||
+ | |||
+ | {{page> | ||
===== Evidence ===== | ===== Evidence ===== | ||
+ | /* Comment out what is not applicable and explain the rest: */ | ||
+ | /* * [[wiki: | ||
- | Widely 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. | + | * [[wiki: |
+ | * [[wiki: | ||
+ | |||
+ | /* * [[wiki: | ||
===== Relations to Other Principles ===== | ===== Relations to Other Principles ===== | ||
- | ==== Specialization of ==== | + | ==== Generalizations |
- | * [[principles: | + | * [[Murphy' |
- | ==== Generalization of ==== | + | ==== Specializations |
- | ==== Contrary to ==== | + | * [[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. | ||
- | ==== Complementary to ==== | + | ==== Contrary Principles |
- | ==== Part of the Following Principle Collections ==== | + | * [[Keep It Simple Stupid]] (KISS): Especially code generators can be very complex. |
+ | ==== Complementary Principles ==== | ||
- | ===== Example | + | * [[Generalization Principle]] (GP): A generalized solution avoids duplication. |
+ | |||
+ | ==== Principle Collections | ||
+ | |||
+ | {{page> | ||
+ | {{page> | ||
+ | |||
+ | ===== Examples | ||
===== Description Status ===== | ===== Description Status ===== | ||
+ | /* Choose one of the following and comment out the rest: */ | ||
+ | / | ||
+ | [[wiki: | ||
+ | / | ||
+ | |||
+ | |||
+ | ===== Further Reading ===== | ||
+ | |||
+ | * [[wiki> | ||
+ | * [[wp> | ||
+ | * [[http:// | ||
- | Stub | + | ===== Discussion ===== |
+ | Discuss this wiki article and the principle on the corresponding [[talk: |
principles/don_t_repeat_yourself.txt · Last modified: 2021-10-18 21:27 by christian