IMPORTANT RULES:
- Maintain the original formatting (markdown, HTML tags, links, etc.)
- Keep technical terms and proper nouns as appropriate
- Preserve code blocks and technical syntax exactly
- Maintain the same tone and style
- Only output the translated text, no explanations or comments
When we modify one instance of a repeated expression, we must not forget that the others should also be updated, and we often overlook or neglect this situation during the process.
DRY (Don’t Repeat Yourself)
Don’t repeat yourself (DRY) is a software development principle designed to eliminate code duplication and the associated risks of redundancy and complexity. The DRY principle emphasizes that “Every piece of knowledge should have a single, definitive, and authoritative representation within a system.” This principle was formulated by Andy Hunt1 and Dave Thomas2 in the book The Pragmatic Programmer3 4.
Dave Thomas says, “While most people limit DRY to avoiding code duplication, the underlying idea goes far beyond that.” Given all these considerations, why don’t we strive to find a single path that represents each feature? The first answer that comes to mind is that if there are multiple ways to achieve the same goal, they are likely to contradict each other. Even if they don’t contradict, there is no guarantee that changes to one will not affect others5.
Successfully applying the DRY principle means considering whether any other logical components of the system will be affected by a change. Additionally, since effects on related logical components can be predicted, the process should be properly managed and changes should be effectively tracked and managed.
Advantages of the DRY Principle
The advantages provided by the DRY principle can be briefly summarized under a few subheadings.
Maintainability
One of the most important benefits of DRY is the prevention of potential conflicts, failures, and errors that may arise due to duplicated code. As a result, any development need or issue arising from a related logic can be easily resolved through a single, centralized area.
Readability
Code written according to the DRY principle is much easier to read / analyze / understand. Of course, this is not directly due to DRY, but rather a result of the developer who analyzes the code with the DRY principle in mind. Because, the developer must provide a more detailed solution by putting forth greater effort in accordance with this principle.
Reusability
By emphasizing the perspective of the DRY principle, code reuse is encouraged instead of code duplication. Reusable code provides a cost advantage by accelerating the development process.
Testability
The more operations involved, the more test code must be written. If there is no code duplication, then fewer test operations are required. Thus, with DRY, more effective and practical testing processes (unit testing and integration testing) will be in place.
WET (Write Everything Twice)
Write Everything Twice (WET) can be described as the opposite of the DRY principle. It is defined by phrases such as “Write everything twice”, “We enjoy writing”, and “We waste everyone’s time”.
Forward Reading
- Keep It DRY, Shy, and Tell the Other Guy [pdf]
- Chapter 30. Don’t Repeat Yourself
- Orthogonality and the DRY Principle
- DRY code vs. WET code
- Once And Only Once
- Don’t Repeat Yourself (DRY)
- The DRY Principle Explained: Its Benefit and Cost with Examples
- Don’t Repeat Yourself is Misunderstood
- Stop trying to be so DRY, instead Write Everything Twice (WET)
- Software Design Principles DRY and KISS