Glossary termPrinciple

Don't Repeat Yourself

Also known as: DRY, DRY principle

Software development principle that every piece of knowledge in a system should have a single, authoritative representation rather than duplicates.

Don't Repeat Yourself (DRY) is a principle aimed at reducing duplication in software. Its core claim is that each piece of knowledge, whether a business rule, a calculation, a configuration value or a data definition, should live in exactly one place in a system. If the sales tax rate appears in ten files, changing it requires finding all ten, and missing one introduces a bug. Defining it once and referring to it everywhere else means a change happens in one spot. DRY applies to code, database schemas, documentation, build scripts and tests alike.

Andy Hunt and Dave Thomas formulated the principle in their 1999 book "The Pragmatic Programmer". They stressed that DRY concerns knowledge and intent, not merely identical lines of text: two pieces of code that look alike but represent different rules may legitimately stay separate, while the same rule expressed both in code and in documentation is a violation. Techniques for applying DRY include extracting functions and modules, using constants and configuration files, generating code or documentation from a single source and building on shared libraries.

DRY is among the most cited principles in software engineering, and its violations are a common form of technical debt. Misapplied, however, it can do harm. Merging code that only happens to look similar creates premature abstractions that couple unrelated parts of a system and become hard to change, and several writers have argued that tolerating some duplication is preferable to an abstraction that does not fit. Many programmers follow a rule of three, extracting shared code only when it appears a third time. The opposite of DRY is jokingly called WET, for write everything twice, and refactoring is the usual remedy.

Key points

  • DRY holds that each piece of knowledge should have a single, authoritative representation in a system.
  • Andy Hunt and Dave Thomas introduced the principle in their 1999 book "The Pragmatic Programmer".
  • The principle targets duplicated knowledge and intent, not merely similar-looking lines of code.
  • Overzealous deduplication can create premature abstractions, so many developers wait for a third repetition.

Related entries

Glossary term

Refactoring

Programming

Process of restructuring existing code to improve its design, readability or maintainability without changing its external behavior.

Glossary term

Abstraction

Programming

Principle of managing complexity by hiding details behind simpler interfaces, so a component can be used without knowing how it works inside.

Glossary term

Technical Debt

Programming

Metaphor for the future cost of rework created when software teams choose a quick or expedient solution instead of a cleaner, more durable one.

Glossary term

Software Library

Programming

Collection of prewritten, reusable code, such as functions and classes, that programs call on to perform common tasks without reimplementing them.

Glossary term

Design Pattern

Programming

Reusable, named solution to a commonly recurring problem in software design, described as a template rather than as finished code.

Glossary term

Rubber Duck Debugging

ProgrammingIdeas and Culture

Debugging method in which a programmer explains code line by line to an inanimate object, often a rubber duck, to expose the source of a bug.

Glossary term

Function that takes other functions as arguments or returns a function as its result, such as map, filter and reduce.