User Tools

Site Tools


principles:don_t_repeat_yourself

Don't Repeat Yourself (DRY)

Variants and Alternative Names

  • Single Point of Truth (SPOT)
  • Single Source of Truth (SSOT)

Context

Principle Statement

“Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”1)

Description

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.

Rationale

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.

Origin

Evidence

  • 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.

Relations to Other Principles

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

Contrary Principles

Complementary Principles

Principle Collections

OOD Principle Language
General Principles Modularization Principles
KISS DRY GP MP IE LC HC

Examples

Description Status

Further Reading

1)
Andrew Hunt and David Thomas: The Pragmatic Programmer: From Journeyman to Master; see List of Tips.
principles/don_t_repeat_yourself.txt · Last modified: 2021-02-02 18:43 by 95.216.157.202