DRY (Don't Repeat Yourself) means every piece of knowledge should have one clear, authoritative place in a system. The idea comes from the book The Pragmatic Programmer. It is about business rules and decisions, not about lines of code that look the same.
Two snippets can look identical but hold different knowledge. For example, a VAT rounding rule and a shipping rounding rule may happen to match today. If you merge them into one helper, you couple two things that change for different reasons. Then this happens:
- one rule changes, so someone adds a flag parameter
- another change adds another flag
- the shared function becomes the wrong abstraction
The wrong abstraction is harder to change than the duplication was.
Good practice:
- Remove duplication of real knowledge, like a tax rule or a validation rule used by several endpoints.
- Accept code that only looks similar by accident. Wait until a few cases show what really varies.
- If a shared abstraction starts to grow flags, inline it back into its callers, then extract it again in a better shape.
- DRY is about one source of truth for knowledge, not about identical text.
- Do not merge code that is only similar by coincidence.
- The wrong abstraction grows flags and couples callers that are not related.
- Prefer duplication over the wrong abstraction. Inline it and extract it again when needed.