ArticleslgStudy

computer science

Don't repeat yourself

Don't repeat yourself is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Don't repeat yourself rather than just read about it. In short: "Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place. The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system".

Key takeaways

  • Don't repeat yourself belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Don't repeat yourself to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Don't repeat yourself from memory before moving on to harder problems.

Reference excerpt

"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place. 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.

Single choice principle A particular case of DRY is the single choice principle. It was defined by Bertrand Meyer as: "Whenever a software system must support a set of alternatives, one and only one module in the system should know their exhaustive list." It was applied when designing Eiffel.

Alternatives

WET The opposing view to DRY is called WET, a backronym 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. This conceptualization of "WET" as an alternative to "DRY" programming has been around since at least 2002 in the Java world, though it is not known who coined the term.

AHA Another approach to abstractions is to avoid hasty abstractions (AHA). AHA is rooted in the understanding that the deeper the investment engineers have made into abstracting a piece of software, the more they perceive that the cost of that investment can never be recovered (sunk cost fallacy). 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. According to Kent C. Dodds, it is optimizing for change first, and avoiding premature optimization. The term was coined by software engineer Cher Scarlett and the practice was described originally by software engineer and author Sandi Metz as "prefer duplication over the wrong abstraction".

See also

References

External links Don't Repeat Yourself at WikiWikiWeb 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) Wilson G, Aruliah DA, Brown CT, Chue Hong NP, Davis M, Guy RT, et al. (2014). "Best Practices for Scientific Computing". PLOS Biol. 12 (1) e1001745. arXiv:1210.0530. doi:10.1371/journal.pbio.1001745. PMC 3886731. PMID 24415924. Don't Repeat Yourself (or Others)

Worked examples

Example 1 — a first encounter with Don't repeat yourself

Start with the simplest possible case. Write down what Don't repeat yourself claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Don't repeat yourself before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Don't repeat yourself ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Don't repeat yourself

In research
Don't repeat yourself appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Don't repeat yourself in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Don't repeat yourself is common in secondary-school and first-year university syllabi. It links to neighbouring topics Computer jargon, Computer programming folklore, Programming principles, so understanding it makes those chapters shorter.
In everyday life
Look for Don't repeat yourself outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Don't repeat yourself” →

How to study Don't repeat yourself in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Don't repeat yourself means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Don't repeat yourself out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Don't repeat yourself in simple terms?

"Don't repeat yourself" (DRY) is a principle of software development aimed at reducing repetition of information which is likely to change, replacing it with abstractions that are less likely to change, or using data normalization which avoids redundancy in the first place. The DRY principle is sta…

Why does Don't repeat yourself matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Don't repeat yourself?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Don't repeat yourself.

Tags

  • Computer jargon
  • Computer programming folklore
  • Programming principles
  • Software engineering folklore

Keep exploring