Don't repeat yourself
"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 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 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.
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. Kevin Greer named and described this programming principle.
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'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). 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, and suggested DATE as an alternative before settling on Cher Scarlett's suggestion of AHA.
- Abstraction principle (programming)
- Code duplication
- Code reuse
- Copy and paste programming
- Database normalization and denormalization
- Disk mirroring
- Loop unrolling
- Redundancy (engineering)
- Rule of three (computer programming)
- Separation of concerns
- Single source of truth (SSOT/SPOT)
- Structured programming
- Two or more, use a for
- You aren't gonna need it (YAGNI)
- Foote, Steven (2014). Learning to Program. Addison-Wesley Professional. p. 336. ISBN 9780133795226.
- Hunt, Andrew; Thomas, David (1999). The Pragmatic Programmer : From Journeyman to Master (1 ed.). USA: Addison-Wesley. pp. 320. ISBN 978-0201616224.
- Dave Thomas, interviewed by Bill Venners (2003-10-10). "Orthogonality and the DRY Principle". Retrieved 2006-12-01.
- Pai, Praseed; Xavier, Shine (2017-01-31). .NET Design Patterns. Packt Publishing Ltd. ISBN 978-1-78646-186-5.
- Justin Lee (2006-03-08). "DRY is for losers". Retrieved 2013-08-31.
- Alex Papadimoulis (2011-12-08). "The WET Cart". Retrieved 2012-05-21.
- Kevin Greer (2016-02-05). "FOAM DRY + WET". YouTube. Archived from the original on 2021-12-21. Retrieved 2016-03-09.
- Kent C. Dodds (2019-04-01). "AHA Programming". Retrieved 2021-05-08.
- Sandi Metz (2016-01-20). "The Wrong Abstraction". Retrieved 2021-05-08.
- Bartholomae, Daniel (21 August 2020). "Moist code - Why code should not be completely DRY". The Startup CTO. Retrieved 11 November 2021.
- Haus, Ev (24 December 2020). "Using DRY, WET & DAMP code". Medium. Retrieved 11 November 2021.
- Fields, Jay. "DRY code, DAMP DSLs". Jay Fields' Thoughts. Retrieved 11 November 2021.
- Resnick, Brian (28 April 2016). "Why do so many people dislike the word "moist"? This scientist has a theory". Vox Media. Retrieved 11 November 2021.
- Dodds, Kent (27 March 2021). "3 Minutes with Kent: Write the code first, then make the abstraction". Briefs. Retrieved 11 November 2021.
- Dodds, Kent; Bostian, Emma; Nisi, Nick (30 July 2021). "JS Party – Episode #186: Getting hooked on React". The Changelog. Retrieved 11 November 2021.
- 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)