"Don't repeat yourself" (DRY) is a
principle of
software development aimed at reducing repetition of software patterns, replacing it with abstractions or using
data normalization
In mathematics and computer science, a canonical, normal, or standard form of a mathematical object is a standard way of presenting that object as a mathematical expression. Often, it is one which provides the simplest representation of an obj ...
to avoid redundancy.
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 Dave may refer to:
Film, television, and theater
* Dave (film), ''Dave'' (film), a 1993 film starring Kevin Kline and Sigourney Weaver
* Dave (musical), ''Dave'' (musical), a 2018 stage musical adaptation of the film
* Dave (TV channel), a digital ...
in their book ''
The Pragmatic Programmer
''The Pragmatic Programmer: From Journeyman to Master'' is a book about computer programming and software engineering, written by Andrew Hunt and David Thomas and published in October 1999. It is used as a textbook in related university courses. ...
''. They apply it quite broadly to include "
database schemas,
test plan A test plan is a document detailing the objectives, resources, and processes for a specific test for a software or hardware product. The plan typically contains a detailed understanding of the eventual workflow.
Test plans
A test plan documents th ...
s, 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
Method ( grc, μέθοδος, methodos) literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In recent centuries it more often means a prescribed process for completing a task. It may refer to:
*Scien ...
and
subroutines in their code, Thomas and Hunt rely on
code generators, automatic build systems, and
scripting language
A scripting language or script language is a programming language that is used to manipulate, customize, and automate the facilities of an existing system. Scripting languages are usually interpreted at runtime rather than compiled.
A scripting ...
s to observe the DRY principle across layers.
Alternatives
WET
The opposing view to DRY is called WET, a
backronym
A backronym is an acronym formed from an already existing word by expanding its letters into the words of a phrase. Backronyms may be invented with either serious or humorous intent, or they may be a type of false etymology or folk etymology. The ...
commonly taken to stand for "write everything twice" (alternatively "write every time", "we enjoy typing" or "waste everyone's time"). WET solutions are common in multi-tiered architectures where a developer may be tasked with, for example, adding a comment field on a form in a web application. The text string "comment" might be repeated in the label, the HTML tag, in a read function name, a private variable, database DDL, queries, and so on. A DRY approach eliminates that redundancy by using frameworks that reduce or eliminate all those editing tasks except the most important ones, leaving the extensibility of adding new knowledge variables in one place.
Kevin Greer named and described this programming principle.
AHA
Another approach to abstractions is the AHA principle. AHA stands for "''Avoid Hasty Abstractions''", described by
Kent C. Dodds as optimizing for change first, and avoiding premature optimization.
and was influenced by
Sandi Metz
Sandi Metz is an American software engineer and author. She is the author of ''Practical Object-Oriented Design in Ruby''. Metz teaches workshops around the country for new and experienced developers, emphasizing good programming habits and pra ...
's "prefer duplication over the wrong abstraction".
AHA is rooted in the understanding that the deeper the investment we've made into abstracting a piece of software, the more we perceive that the cost of that investment can never be recovered (
sunk cost fallacy
In economics and business decision-making, a sunk cost (also known as retrospective cost) is a cost that has already been incurred and cannot be recovered. Sunk costs are contrasted with '' prospective costs'', which are future costs that may be ...
). Thus, engineers tend to continue to iterate on the same abstraction each time the requirement changes. AHA programming assumes that both WET and DRY solutions inevitably create software that is rigid and difficult to maintain. Instead of starting with an abstraction, or abstracting at a specific number of duplications, software can be more flexible and robust if abstraction is done when it is needed, or, when the duplication itself has become the barrier and it is known how the abstraction needs to function.
AHA programming was originally named MOIST by Dodds, later again by Daniel Bartholomae, and originally referred to as DAMP by Matt Ryer. There was a different programming principle already named DAMP and described by Jay Fields, and the community pushed back against the usage of MOIST, due to the cultural aversion to the word "moist". Dodds called for alternatives on
Twitter
Twitter is an online social media and social networking service owned and operated by American company Twitter, Inc., on which users post and interact with 280-character-long messages known as "tweets". Registered users can post, like, and ...
, and suggested DATE as an alternative before settling on
Cher Scarlett
Cher Scarlett (born ) is an American software engineer and writer. She is a workers' rights activist and has organized staff at Apple, Activision Blizzard, and Starbucks.
Scarlett, who has bipolar disorder, experienced struggles in her early l ...
's suggestion of AHA.
See also
*
Abstraction principle (programming) In software engineering and programming language theory, the abstraction principle (or the principle of abstraction) is a basic dictum that aims to reduce duplication of information in a program (usually with emphasis on code duplication) whenever ...
*
Code duplication In computer programming, duplicate code is a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity. Duplicate code is generally considered undesirable for a ...
*
Code reuse
In software development (and computer programming in general), code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software, following the reusability principles.
Code reuse may be achie ...
*
Copy and paste programming
Copy may refer to:
*Copying or the product of copying (including the plural "copies"); the duplication of information or an artifact
**Cut, copy and paste, a method of reproducing text or other data in computing
**File copying
**Photocopying, a pr ...
*
Database normalization
Database normalization or database normalisation (see spelling differences) is the process of structuring a relational database in accordance with a series of so-called normal forms in order to reduce data redundancy and improve data integrity ...
and
denormalization
Denormalization is a strategy used on a previously- normalized database to increase performance. In computing, denormalization is the process of trying to improve the read performance of a database, at the expense of losing some write performanc ...
*
Disk mirroring
In data storage, disk mirroring is the replication of logical disk volumes onto separate physical hard disks in real time to ensure continuous availability. It is most commonly used in RAID 1. A mirrored volume is a complete logical represen ...
*
Loop unrolling
Loop unrolling, also known as loop unwinding, is a loop transformation technique that attempts to optimize a program's execution speed at the expense of its binary size, which is an approach known as space–time tradeoff. The transformation ...
*
Redundancy (engineering)
*
Rule of three (computer programming)
*
Separation of concerns
In computer science, separation of concerns is a design principle for separating a computer program into distinct sections. Each section addresses a separate '' concern'', a set of information that affects the code of a computer program. A concern ...
*
Single source of truth
In information science and information technology, single source of truth (SSOT) architecture, or single point of truth (SPOT) architecture, for information systems is the practice of structuring information models and associated data schemas su ...
(SSOT/SPOT)
*
Structured programming
*
Two or more, use a for
*
You aren't gonna need it
"You aren't gonna need it" (YAGNI) is a principle which arose from extreme programming (XP) that states a programmer should not add functionality until deemed necessary. Other forms of the phrase include "You aren't going to need it" (YAGTNI) and ...
(YAGNI)
References
External links
*
Don't Repeat Yourself at
WikiWikiWeb
The WikiWikiWeb is the first wiki, or user-editable website. It was launched on 25 March 1995 by programmer Ward Cunningham to accompany the Portland Pattern Repository website discussing software design patterns. The name ''WikiWikiWeb'' ori ...
*
Once and Only Once at WikiWikiWeb
97 Things Every Programmer Should Know (O'Reilly)The myth of over-normalization(discussion of academic extremes vs. real-world database scenarios)
*
{{DEFAULTSORT:Don't Repeat Yourself
Software engineering folklore
Computer programming folklore
Programming principles