DRY forbids duplicating knowledge, not code. Identical code representing different concepts that change independently isn't a violation—it's appropriate separation.
DRY = One source of truth per concept.
Benefits
Single-point maintenance — fix or update once, applied everywhere
Fewer bugs — no inconsistent copies drifting out of sync
Better readability — modular, organized code
Easier testing — test one unit, trust it everywhere
Consistent behavior — one source of truth per concept
The Key Distinction: Knowledge vs. Code Duplication
DRY targets knowledge duplication, not code duplication.
Identical-looking code isn’t a violation if it represents different concepts that should evolve independently. Two functions with the same implementation today might change for completely different reasons tomorrow—coupling them prematurely creates the wrong abstraction.
Not All Code Duplication Is Knowledge Duplication
As part of your online wine ordering application you’re capturing and validating your user’s age, along with the quantity they’re ordering. According to the site owner, they should both be numbers, and both greater than zero. So you code up the validations:
During code review, the resident know-it-all bounces this code, claiming it’s a DRY violation: both function bodies are the same.
They are wrong. The code is the same, but the knowledge they represent is different. The two functions validate two separate things that just happen to have the same rules. That’s a coincidence, not a duplication. - pragmatic-programmer
“A little copying is better than a little dependency” — Go Proverbs
Rule of Three: Wait until you see duplication three times before abstracting. Premature abstraction often creates worse problems than duplication. - rule-of-3
Perils of WET (Write Everything Twice) code
vs-WET-write-everything-twice-perils
WET (Write Everything Twice) Perils:
Maintenance Nightmare: With duplicated code, any change needs to be replicated across all instances. This makes maintenance cumbersome and error-prone.
Bug Propagation: If a bug exists in a duplicated code segment, it’s likely present everywhere the code is replicated. This can lead to widespread system vulnerabilities.
Increased Complexity: WET code often leads to bloated, complex systems that are hard to understand and navigate, making it challenging for new developers to onboard.
Scalability Issues: As the system grows, so does the duplicated code, leading to heavier, less manageable, and slower applications.
Inconsistency Risks: It’s easy to update one instance of the code and miss others, leading to inconsistent behavior across the application.
Poor Optimization: Duplicate code makes it harder to implement optimizations. Changes meant to improve performance must be applied in multiple places, increasing the risk of missing some.
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. — Wikipedia