Avoid Hasty Abstractions
   HOME

TheInfoList



OR:

"Don't repeat yourself" (DRY) is a
principle A principle may relate to a fundamental truth or proposition that serves as the foundation for a system of beliefs or behavior or a chain of reasoning. They provide a guide for behavior or evaluation. A principle can make values explicit, so t ...
of
software development Software development is the process of designing and Implementation, implementing a software solution to Computer user satisfaction, satisfy a User (computing), user. The process is more encompassing than Computer programming, programming, wri ...
aimed at reducing repetition of
information Information is an Abstraction, abstract concept that refers to something which has the power Communication, to inform. At the most fundamental level, it pertains to the Interpretation (philosophy), interpretation (perhaps Interpretation (log ...
which is likely to change, replacing it with
abstractions Abstraction is a process where general rules and concepts are derived from the use and classifying of specific examples, literal ( real or concrete) signifiers, first principles, or other methods. "An abstraction" is the outcome of this process ...
that are less likely to change, 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 obje ...
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 ''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 schema The database schema is the structure of a database described in a formal language supported typically by a relational database management system (RDBMS). The term "wikt:schema, schema" refers to the organization of data as a blueprint of how the ...
s,
test plan A test plan is a document detailing the objectives, resources, and processes for a specific test session for a software or hardware product. The plan typically contains a detailed understanding of the eventual workflow. Test plans A test plan docu ...
s, the build system, even
documentation Documentation is any communicable material that is used to describe, explain or instruct regarding some attributes of an object, system or procedure, such as its parts, assembly, installation, maintenance, and use. As a form of knowledge managem ...
. 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 Sync and synch are abbreviations of synchrony, or synchronization, the coordination of events to keep them in time. The opposite of synchrony is asynchrony. Sync or synch may also refer to: Computing and technology *Sync (Unix), a command and a ...
. Besides using
methods Method (, methodos, from μετά/meta "in pursuit or quest of" + ὁδός/hodos "a method, system; a way or manner" of doing, saying, etc.), literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In re ...
and
subroutine In computer programming, a function (also procedure, method, subroutine, routine, or subprogram) is a callable unit of software logic that has a well-defined interface and behavior and can be invoked multiple times. Callable units provide a ...
s in their code, Thomas and Hunt rely on code generators, automatic build systems, and
scripting language In computing, a script is a relatively short and simple set of instructions that typically automation, automate an otherwise manual process. The act of writing a script is called scripting. A scripting language or script language is a programming ...
s 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 Bertrand Meyer (; ; born 21 November 1950) is a French academic, author, and consultant in the field of computer languages. He created the Eiffel programming language and the concept of design by contract. Education and academic career 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 Eiffel may refer to: Places * Eiffel Tower, in Paris, France, designed by Gustave Eiffel *:* Champ de Mars – Tour Eiffel station, Metro station serving the Eiffel Tower * Eiffel Bridge, Ungheni, Moldova, designed by Gustave Eiffel * Eiffel Bri ...
.


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. 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 the AHA principle. AHA stands for ''avoid hasty abstractions'', described b
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 pr ...
's "prefer duplication over the wrong abstraction". 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. AHA programming was originally named "moist code" by Dodds, later again by Daniel Bartholomae, and originally referred to as DAMP (''Don't Abstract Methods Prematurely'') by Matt Ryer. There was a different programming principle already named DAMP (''Descriptive And Meaningful Phrases'') 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, officially known as X since 2023, is an American microblogging and social networking service. It is one of the world's largest social media platforms and one of the most-visited websites. Users can share short text messages, image ...
, and suggested DATE as an alternative before settling on
Cher Scarlett Cher Scarlett (born ) is an American software engineer, workers' rights activist, and writer who is known for starting #AppleToo. She has organized staff at Apple, Activision Blizzard, and Starbucks. Scarlett, who has bipolar disorder, experi ...
'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 p ...
*
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 n ...
*
Code reuse Code reuse is the practice of using existing source code to develop software instead of writing new code. ''Software reuse'' is a broader term that implies using any existing software asset to develop software instead of developing it again. An as ...
*
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 p ...
*
Database normalization Database normalization 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. It was first proposed by British computer scien ...
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 performance, ...
*
Disk mirroring In Data storage device, data storage, disk mirroring is the Replication (computing), replication of logical disk volumes onto separate physical hard disks in Real-time computing, real time to ensure continuous availability. It is most commonly u ...
*
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 c ...
*
Redundancy (engineering) In engineering and systems theory, redundancy is the intentional duplication of critical components or functions of a system with the goal of increasing reliability of the system, usually in the form of a backup or fail-safe, or to improve a ...
*
Rule of three (computer programming) Rule of three (''"Three strikes and you refactor"'') is a code refactoring rule of thumb to decide when similar pieces of code should be refactored to avoid duplication. It states that two instances of similar code do not require refactoring, but ...
*
Separation of concerns In computer science, separation of concerns (sometimes abbreviated as SoC) 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 o ...
*
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 s ...
(SSOT/SPOT) *
Structured programming Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making specific disciplined use of the structured control flow constructs of selection ( if/then/else) and repet ...
* 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 "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 r ...
at
WikiWikiWeb The WikiWikiWeb is the first wiki, or user-editable website. It was launched on 25 March 1995 by programmer Ward Cunningham and has been a read-only archive since 2015. The name ''WikiWikiWeb'' originally also applied to the wiki software that o ...
* 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