Write Everything Twice (WET)
Code duplication is often frowned upon. However, it can be more beneficial to have duplicated code rather than introducing premature abstraction.
Usage
π Guideline
Write Everything Twice (WET): Writing code with duplication is preferable to premature abstraction.
Code duplication is often frowned upon in software development, as it violates the Don't Repeat Yourself (DRY) principle. However, in certain situations, it can be more beneficial to have duplicated code rather than introducing premature abstraction.
π οΈ How to Apply
- Avoid premature abstraction: Before extracting common code into reusable abstractions, ensure that the similarities between the duplicated code are well understood and stable. π«
- Consider code readability: Duplicated code can sometimes be more readable and easier to understand, especially when the logic is straightforward and self-contained. π
- Strive for balance: While duplication should be minimized, it's essential to find the right balance between abstraction and duplication. Evaluate the trade-offs between the benefits of abstraction and the potential downsides. βοΈ
Pros and Cons
π Pros
- Code Clarity: Duplicating code can sometimes lead to clearer and more readable code, especially when the duplicated logic is straightforward and self-contained. π
- Flexibility: Duplicated code allows individual components to evolve independently without affecting others. It provides the flexibility to modify specific sections without introducing complex abstractions. π§©
- Simplicity: Duplication eliminates the need for complex abstractions, making the code simpler and easier to understand, especially for beginners. π―
π Cons
- Code Maintainability: Duplicated code requires changes to be applied to multiple places, increasing the maintenance effort and the risk of inconsistencies. π§
- Potential code bloat: With excessive duplication, the codebase can become larger and more challenging to maintain. It can result in redundant logic, increased file sizes, and decreased overall code quality. π
Examples
β Bad
// Bad example: Over abstraction to avoid repetition
function calculateArea(radius: number): number {
return calculatePiTimesRadius(radius) * radius;
}
function calculateCircumference(radius: number): number {
return 2 * calculatePiTimesRadius(radius);
}
function calculatePiTimesRadius(radius: number): number {
return 3.14 * radius;
}
const area = calculateArea(5);
const circumference = calculateCircumference(5);
β Good
// Good example: Duplication that's clearer and more straightforward
function calculateArea(radius: number): number {
return 3.14 * radius * radius;
}
function calculateCircumference(radius: number): number {
return 2 * 3.14 * radius;
}
const area: = calculateArea(5);
const circumference = calculateCircumference(5);
References
π Related principles
- DRY (Don't Repeat Yourself): The principle of Write Everything Twice (WET) goes against the DRY principle, which emphasizes code reuse and minimizing duplication. While DRY is generally preferred, WET can be a pragmatic approach in specific scenarios. πͺοΈ
- Keep It Simple, Stupid (KISS): By keeping code duplication when it improves clarity, WET adheres to the spirit of KISS. π€
- YAGNI (You Ain't Gonna Need It): By not prematurely abstracting code, WET focuses on fulfilling the current requirements without adding unnecessary complexity. ποΈ