DRY vs WET Code: When Repeating Yourself Is Actually Better
Every developer has heard the advice: "Don't Repeat Yourself." But what if following that principle too aggressively makes your code harder to maintain? Sometimes, duplicating a few lines of code is better than creating an abstraction that complicates everything. This is where DRY vs WET becomes interesting. What Is DRY Code? DRY (Don't Repeat Yourself) is a software development principle that…
Repetitive lines of code can sometimes be a good thing, according to a piece of advice that challenges the conventional wisdom of "Don't Repeat Yourself." This concept, known as DRY vs WET code, presents a nuanced view of when it's beneficial to duplicate code rather than always striving for abstraction.
DRY stands for "Don't Repeat Yourself," a principle that advocates for minimizing duplication by having just one authoritative representation of every piece of knowledge within a system. For example, in C#, instead of defining two separate methods that both calculate tax in the same way, DRY would suggest creating a single method. While this leads to less duplicated code and easier maintenance overall, the principle isn't without its pitfalls.
WET, or "Write Everything Twice," is the opposite of DRY. It refers to code that repeats itself rather than being abstracted into a unified form. At first glance, WET might seem like a code maintenance nightmare, but there are situations where a little duplication can actually improve understandability. For instance, two validation methods that check for non-empty emails might seem like candidates for abstraction.
However, if these methods need to evolve in different ways in the future, keeping them separate can be advantageous. This approach avoids unnecessary coupling between the code.
The "Rule of Three" is a practical guideline in this debate. The rule suggests examining code duplication at three points: the first occurrence should be written as-is, the second occurrence should prompt a notice but not an immediate abstraction, and the third occurrence should evaluate whether a shared abstraction is necessary. This heuristic helps developers avoid premature abstraction, which can introduce complexity and make future changes more difficult.
When to choose DRY:
- Multiple components share the same business rule.
- Duplicated logic must always remain consistent.
- Changes should propagate across all consumers.
- The abstraction has a clear responsibility.
When to choose WET:
- Similar code serves different business purposes.
- Requirements are likely to evolve independently.
- Extracting shared logic introduces unnecessary complexity.
- A little duplication makes the code easier to understand.
The conclusion from this debate is that good software engineering involves more than just minimizing duplicated code. It's about finding the right balance between reusability, readability, maintainability, and flexibility. While DRY is valuable for preventing duplicated knowledge, there are times when deliberately using WET code can prevent the creation of an abstraction that might not suit future needs.
In essence, duplicating code can sometimes be a wiser choice than committing to an abstraction that might not hold up to future changes.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.