Don't repeat yourself

From Wikipedia, the free encyclopedia

  (Redirected from DRY code)
Jump to: navigation, search
Contrast with redundancy and mirror.

In software engineering, Don't Repeat Yourself (DRY) or Duplication is Evil (DIE) is a principle of software development aimed at reducing repetition of information of all kinds, especially useful in multi-tier architectures. 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."[1] When the DRY principle is applied successfully, a modification of any single element of a system does not change 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.

Contents

[edit] Applying DRY

The DRY code philosophy is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." This philosophy is also prevalent in model-driven architectures, in which software artifacts are derived from a central object model expressed in a form such as UML. DRY code is created by data transformation and code generators, which allows the software developer to avoid copy and paste operations. DRY code usually makes large software systems easier to maintain, as long as the data transformations are easy to create and maintain. Tools such as XDoclet and XSLT are examples of DRY coding techniques. Examples of systems that require duplicate information are Enterprise Java Beans version 2, which requires duplication not just in Java code but also in configuration files. Examples of systems that attempt to reduce duplicate information include the Ruby on Rails application development environment and Enterprise Java Beans version 3.

[edit] Once and Only Once

DRY is broader than Once and Only Once (OAOO). OAOO speaks to the functional behavior of the code, and is the reason for object composition and the implementation of inheritance in object oriented languages. DRY is not about just avoiding code duplication, but more generally about avoiding multiple and possibly diverging ways to express every piece of knowledge: e.g., logic, database schemas, and constants.

[edit] When DRY may not be advantageous

DRY employed automatically can lead to a problem because the underlying premise of DRY is flawed. The flaw is the assumption that because information is the same in more than one place that it is logically related. Identical information can be incidentally identical and the separate copies may have their own quite distinct raison d'etre. Thus as time goes forward they will evolve in different ways with no reason for there to be synchronisation. DRY needs to be preceded by a careful anaylysis to determine where it is applicable. Building information systems is too subtle for such simplistic notions as automatic DRY.

In some contexts, the effort required to enforce the DRY philosophy may be greater than the effort to maintain separate copies of the data. In some other contexts, duplicated information is immutable or kept under a control tight enough to make DRY not required.

  • Imposing standards aimed at strict adherence to DRY could stifle community involvement in contexts where it is highly valued, such as a wiki.[citation needed]
  • Configuration management and version control tools allow multiple and diverging copies ("branches"). For example, good practice generally involves development, testing, and production using different source code bases so that ongoing development and testing do not affect production. Ideally, divergence is temporary; differences are kept reasonably small and eventually reconciled ("merged") with the help of the tools.
  • Human-readable documentation (from code comments to printed manuals) are typically a restatement of something in the code with elaboration and explanation for those who do not have the ability or time to read and internalize the code. However, DRY holds that if the human-readable document adds no value except the change in format, then the effort should be made to generate it rather than write it.
  • Source code generation - Non-duplication could be useful for input to the source code generator, but not for the result, if the duplicate information is never modified manually.
  • Limitations of a programming language can sometimes make it to easier to duplicate code. For instance, the two functions addAll(myGraph) and multiplyAll(myGraph) duplicate the code to browse the same type of graph. The DRY principle recommends refactoring to applyAll(myGraph, add) and applyAll(myGraph, multiply). However, in languages with poor functional support, such a refactoring is likely to be costly to implement, and to produce verbose and confusing code.

[edit] See also

[edit] References

  1. ^ Dave Thomas, interviewed by Bill Venners (2003-10-10). "Orthogonality and the DRY Principle". http://www.artima.com/intv/dry.html. Retrieved 2006-12-01. 

[edit] External links