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 revision Previous revision
Next revision
Previous revision
principles:don_t_repeat_yourself [2012-12-18 15:19]
christian further relations
principles:don_t_repeat_yourself [2015-06-26 15:45]
207.107.205.22 [Strategies]
Line 9: Line 9:
  
   * [[contexts:​Object-Oriented Design]]   * [[contexts:​Object-Oriented Design]]
 +  * [[contexts:​Implementation]]
 ===== Principle Statement ===== ===== Principle Statement =====
  
Line 27: Line 27:
 ===== Strategies ===== ===== Strategies =====
  
 +  * 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
  
-===== Origin ​=====+===== Caveats ​=====
  
-Andrew Hunt and David Thomas: //[[http://​pragprog.com/​book/​tpp/​the-pragmatic-programmer|The Pragmatic Programmer: From Journeyman to Master]]//+See section ​[[#contrary principles]].
  
  
 +===== Origin =====
 +
 +{{page>​resources:​The Pragmatic Programmer#​reference}}
 ===== 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:​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:​Examined]]: There is extensive research on code cloning, its reasons, automatic detection of code clones, their evolution, etc. In ((Chanchal Kumar Roy and James R. Cordy: ​//​[[http://​citeseerx.ist.psu.edu/​viewdoc/​summary;​jsessionid=EBC3E3E783AE0B5DEF75B9EDD4F1C9E2?​doi=10.1.1.62.7869|A survey on software clone detection research]]//​)) 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. ((Elmar Juergens, Florian Deissenboeck,​ Benjamin Hummel, and Stefan Wagner //​[[http://​www4.in.tum.de/​~deissenb/​publications/​2009_juergens_inconsistens_clones.pdf|Do code clones matter?​]]//​)) 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. 
-/​*[[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. On the other hand //The Pragmatic Programmer//​ is a well known book which makes DRY a well-known and accepted principle. 
 + 
 +/*  ​* [[wiki:​Questioned]]*/​
  
  
Line 45: Line 54:
 ==== Generalizations ==== ==== Generalizations ====
  
 +  * [[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:​Generalization Principle]]:​ A generalized solution avoids duplication. +  * [[Once And Only Once]] ​(OAOO): This is the aspect of DRY which is concerned with the avoidance of (code) duplication. 
-  * [[principles:​Once And Only Once]]: 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. 
-  * [[principles:Single Choice Principle]]:​ 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.
-  * [[principles: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:​OOD Principle Language#​Box}}
 +{{page>​collections:​Principles in "The Pragmatic Programmer"#​Box}}
  
-===== Example ​=====+===== Examples ​=====
  
  
 ===== Description Status ===== ===== Description Status =====
 /* Choose one of the following and comment out the rest: */ /* Choose one of the following and comment out the rest: */
-[[wiki:​Stub]] +/*[[wiki:​Stub]]*
-/*[[wiki:​Incomplete]]*/+[[wiki:​Incomplete]]
 /​*[[wiki:​Complete]]*/​ /​*[[wiki:​Complete]]*/​
  
Line 80: Line 91:
   * [[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]]
  
 +===== Discussion =====
 +
 +Discuss this wiki article and the principle on the corresponding [[talk:​principles:​Don'​t Repeat Yourself|talk page]].
principles/don_t_repeat_yourself.txt · Last modified: 2015-06-26 15:45 by 207.107.205.22