| name | dry |
| description | When writing or reviewing code to eliminate duplicated knowledge and business logic. Use when the user says "this is duplicated," "we have this in two places," "single source of truth," "DRY this up," or "shotgun surgery." For premature abstraction concerns, see yagni. |
| metadata | {"version":"1.0.0"} |
DRY — Don't Repeat Yourself
Before Applying
If .agents/stack-context.md exists, read it first. Apply this principle using idiomatic patterns for the detected stack. For framework-specific details, use context7 MCP or web search — don't guess.
Principle
Every piece of knowledge in a system should have a single, authoritative representation. When that knowledge changes, you should only need to change it in one place.
Why This Matters in Production
Duplicated logic is a ticking time bomb. When a business rule changes and you update it in one place but miss the copy in another, you get inconsistent behavior that's hard to detect and harder to debug. The more copies exist, the more likely one diverges silently.
DRY is not about eliminating similar-looking code. It's about eliminating duplicated knowledge — the same business rule, the same decision, the same source of truth expressed in multiple places.
Rules
- Distinguish knowledge duplication from code duplication. Two functions that look identical but represent different business concepts should stay separate. Two functions that encode the same business rule should be unified.
- Single source of truth for data. Configuration, constants, schema definitions, and validation rules should each live in exactly one place. Everything else should derive from that source.
- Extract when the pattern is stable. Don't extract on the first occurrence — you don't yet know the right shape of the abstraction. Extract when you've seen the pattern repeat with the same semantics at least twice.