User Tools

Site Tools


principles:don_t_repeat_yourself

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
principles:don_t_repeat_yourself [2021-09-02 11:47] – old revision restored (2021-05-19 10:22) 65.21.179.175principles:don_t_repeat_yourself [2021-09-02 11:47] – old revision restored (2021-05-11 19:02) 65.21.179.175
Line 10: Line 10:
   * [[contexts:Object-Oriented Design]]   * [[contexts:Object-Oriented Design]]
  
-===== Definition =====+===== Principle Statement =====
  
 > "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."((Andrew Hunt and David Thomas: //The Pragmatic Programmer: From Journeyman to Master//; see [[http://pragprog.com/the-pragmatic-programmer/extracts/tips|List of Tips]].)) > "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."((Andrew Hunt and David Thomas: //The Pragmatic Programmer: From Journeyman to Master//; see [[http://pragprog.com/the-pragmatic-programmer/extracts/tips|List of Tips]].))
Line 23: Line 23:
  
 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. 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.
 +
 +
 +===== Strategies =====
 +
 +  * Add a new invokable module (a function, a method, a class, etc.) instead of duplicating code
 +  * 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]].
  
  
Line 32: Line 43:
 ===== Evidence ===== ===== Evidence =====
 /* Comment out what is not applicable and explain the rest: */ /* Comment out what is not applicable and explain the rest: */
-/*[[wiki:Proposed]]*/ +/*  * [[wiki:Proposed]]*/ 
-/*[[wiki:Examined]]*/ +/*  * [[wiki:Examined]]*/ 
-[[wiki: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:Questioned]]*/+  * [[wiki: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:Questioned]]*/
  
  
Line 42: Line 55:
 ==== Generalizations ==== ==== Generalizations ====
  
-  * [[principles:Generalization Principle]]+  * [[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.
  
 ==== Specializations ==== ==== Specializations ====
  
-  * [[principles:Once And Only Once]]: This is the aspect of DRY which is concerned with the avoidance of (code) 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.
  
 ==== Contrary Principles ==== ==== Contrary Principles ====
  
-  * [[principles:Keep It Simple Stupid]]: Especially code generators can be very complex.+  * [[Keep It Simple Stupid]] (KISS): Especially code generators can be very complex.
  
 ==== Complementary Principles ==== ==== Complementary Principles ====
  
 +  * [[Generalization Principle]] (GP): A generalized solution avoids duplication.
  
 ==== Principle Collections ==== ==== Principle Collections ====
  
 +{{page>collections:OOD Principle Language#Box}}
 +{{page>collections:Principles in "The Pragmatic Programmer"#Box}}
  
 ===== Example ===== ===== Example =====
Line 73: Line 91:
   * [[wp>Don't repeat yourself]]   * [[wp>Don't repeat yourself]]
   * [[http://programmer.97things.oreilly.com/wiki/index.php/Don't_Repeat_Yourself|97things: Don't_Repeat_Yourself]]   * [[http://programmer.97things.oreilly.com/wiki/index.php/Don't_Repeat_Yourself|97things: Don't_Repeat_Yourself]]
 +
principles/don_t_repeat_yourself.txt · Last modified: 2021-10-18 21:27 by christian