(^60) 97 Things Every Programmer Should Know
Don’t Repeat Yourself ............................
Steve Smith
OHELLF A T PRiNCiPLES OF PROGRAMMiNG, Don’t Repeat Yourself (DRY) is
perhaps one of the most fundamental. The principle was formulated by Andy Hunt
and Dave Thomas in The Pragmatic Programmer, and underlies many other well-
known software development best practices and design patterns. The developer
who learns to recognize duplication, and understands how to eliminate it through
appropriate practice and proper abstraction, can produce much cleaner code than
one who continuously infects the application with unnecessary repetition.
Duplication is Waste
Every line of code that goes into an application must be maintained, and is a
potential source of future bugs. Duplication needlessly bloats the codebase,
resulting in more opportunities for bugs and adding accidental complexity to
the system. The bloat that duplication adds to the system also makes it more
difficult for developers working with the system to fully understand the entire
system, or to be certain that changes made in one location do not also need
to be made in other places that duplicate the logic they are working on. DRY
requires that “every piece of knowledge must have a single, unambiguous,
authoritative representation within a system.”
Repetition in Process Calls for Automation
Many processes in software development are repetitive and easily automated.
The DRY principle applies in these contexts, as well as in the source code of
the application. Manual testing is slow, error-prone, and difficult to repeat,
so automated test suites should be used where possible. Integrating software
can be time consuming and error-prone if done manually, so a build process
should be run as frequently as possible, ideally with every check-in. Wherever
painful manual processes exist that can be automated, they should be auto-
mated and standardized. The goal is to ensure that there is only one way of
accomplishing the task, and it is as painless as possible.