Don't repeat yourself (DRY)

Don't repeat yourself (DRY)

The Don't Repeat Yourself (DRY) rule is a core idea in software engineering that says: Every piece of knowledge or logic in a system should have a single, unambiguous source of truth.

Duplication (inadvertent or purposeful duplication) can lead to maintenance nightmares, poor factoring, and logical contradictions. Duplication, and the strong possibility of eventual contradiction, can arise anywhere: in architecture, requirements, code, or documentation. The effects can range from mis-implemented code and developer confusion to complete system failure. The DRY (Don't Repeat Yourself) Principle states: Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.

Why DRY matters?

Repeating code might feel faster at first, but it creates long-term problems:

• If you fix a bug in one place, you must remember to fix it everywhere
• Changes become error-prone
• Code becomes harder to understand and maintain

DRY helps keep code:

• Cleaner
• Easier to maintain
• Less error-prone

When to apply DRY?

Apply DRY when:

• You see the same logic repeated multiple times
• Multiple parts of code depend on the same rule or calculation
• You’re copying and pasting code

But here’s the nuance: Don’t force DRY too early. Premature abstraction can make code harder to read.

Common Ways to Achieve DRY

1. Functions

Instead of repeating logic, create methods:

int add(int a, int b) {
    return a + b;
}

2. Classes / Abstraction

Encapsulate shared behavior:

class Logger {
public:
    void log(std::string msg);
};

3. Constants

Avoid repeating magic numbers:

const int MAX_USERS = 100;

4. Templates / Generics

Reuse logic across types (especially in C++)

5. Modules / Libraries

Move shared logic into reusable components (e.g., Boost, utilities)

Advantages

• Easier maintenance
• Fewer bugs
• Centralized logic
• Better readability (when done right)

Disadvantages (or pitfalls)

DRY can go wrong if overused:

• Over-abstraction → code becomes hard to follow
• Premature optimization → abstracting before patterns are clear
• Tight coupling → too much reuse can make components dependent

This is why developers often balance DRY with another principle:

• You Aren't Gonna Need It (YAGNI) → don’t build abstractions until they’re actually needed

Don't repeat yourself

In software engineering, don't repeat yourself (DRY) is a principle of software development, a principle 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."

When the DRY principle is applied successfully, a modification of any single element of a system does not require a change in 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.

Applying DRY Rule

Also known as Single Source of Truth, this philosophy is 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. An example of a system that requires duplicate information is 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 Symfony, web2py, Yii, Play Framework and Django web frameworks, EiffelStudio, Ruby on Rails application development environment, Microsoft Visual Studio LightSwitch and Enterprise Java Beans version 3.

DRY vs WET solutions

Violations of DRY are typically referred to as WET solutions, which is commonly taken to stand for either "write everything twice" or "we enjoy typing".

WET = “Write Everything Twice” (or worse)

Contents related to 'Don't repeat yourself (DRY)'

There's more than one way to do it (TMTOWTDI)
There's more than one way to do it (TMTOWTDI)