Skip to content

don’t repeat yourself (DRY)

Don’t repeat yourself, or DRY, is a design principle that says every piece of knowledge in a system should live in exactly one place, so a rule can be changed in a single edit instead of chased across five files. It comes from David Thomas and Andrew Hunt’s The Pragmatic Programmer, whose fifteenth tip states it as “Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.”

Say a store ships orders free above fifty dollars, and that rule is written down separately in the checkout and in the cart banner:

Language: Python
def shipping_cost(subtotal):
    return 0.00 if subtotal >= 50 else 6.95


def cart_banner(subtotal):
    if subtotal >= 50:
        return "Your order ships free"
    return f"Add ${50 - subtotal:.2f} to ship free"

Marketing then raises the threshold to seventy-five dollars. Both copies have to move together, along with the third one in the confirmation email that nobody thought to grep for. A DRY version writes the rule down once and calls it from everywhere else:

Language: Python
FREE_SHIPPING_MINIMUM = 50.00


def ships_free(subtotal):
    return subtotal >= FREE_SHIPPING_MINIMUM


def shipping_cost(subtotal):
    return 0.00 if ships_free(subtotal) else 6.95


def cart_banner(subtotal):
    if ships_free(subtotal):
        return "Your order ships free"
    return f"Add ${FREE_SHIPPING_MINIMUM - subtotal:.2f} to ship free"

The threshold now moves in one edit, and nothing downstream can contradict it. Dragging marketing’s new threshold below shows what the checkout, the cart banner, and the confirmation email each tell the same $60.00 cart, first with the rule copied into all three and then with one constant behind them:

Interactive diagram — enable JavaScript to view.

How It Shows Up in Practice

A Python developer meets DRY most often in code review, as a comment on a block that was copied and pasted with two words changed. The repairs are ordinary refactoring moves: extract the shared logic into a function, hoist a repeated literal into a constant, or pull a duplicated method up into a base class.

The principle reaches well past application code, because knowledge leaks into every artifact a project keeps. A version number written in both pyproject.toml and __init__.py, a database schema mirrored by hand in a model class, and a deploy checklist that restates what the pipeline already does are all the same failure in different clothes.

Much of the surrounding toolchain exists to collapse those copies. Code generators derive clients from a single API schema, infrastructure as code keeps one description of the servers, and importlib.metadata.version() reads a package version from the metadata rather than a second hardcoded string.

Common Pitfalls

The usual misreading is treating DRY as a ban on similar-looking code. The principle governs duplicated knowledge rather than duplicated characters. Thomas has said the original idea “was nothing to do with code, it was to do with knowledge.” Two functions that share a shape while answering to different rules aren’t a violation, and merging them couples decisions that will later drift apart.

Hunting textual duplication instead tends to produce an abstraction that fits neither caller. Sandi Metz puts the tradeoff as “duplication is far cheaper than the wrong abstraction,” since a bad abstraction collects flags and branches until unpicking it costs more than the copies ever did. A common guard is to wait for a third occurrence before extracting anything, which gives the pattern time to show which parts actually repeat.

Both failure modes end in the same place. Scattered copies and a forced abstraction each add technical debt, and the difference is only which one a team notices first.

Refactoring Python Applications for Simplicity

Tutorial

Refactoring Python Applications for Simplicity

In this step-by-step tutorial, you'll learn how to refactor your Python application to be simpler and more maintainable and have fewer bugs. You'll cover code metrics, refactoring tools, and common anti-patterns.

intermediate best-practices

For additional information on related topics, take a look at the following resources:

Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.


By Martin Breuss • Updated Oct. 23, 2026