Purpose: Stop and think before creating new code - does it already exist?
Duplicated code is the #1 source of inconsistency bugs.
When you copy-paste or rewrite existing logic:
# Search for similar function names
grep -r "functionName" .
# Search for similar logic
grep -r "keyword" .
| Question | If Yes... |
|---|---|
| Does a similar function exist? | Use or extend it |
| Is this pattern used elsewhere? | Follow the existing pattern |
| Could this be a shared utility? | Create it in the right place |
| Am I copying code from another file? | STOP - extract to shared |
Bad: Copying a validation function to another file
Good: Extract to shared utilities, import where needed
Bad: Creating a new component that's 80% similar to existing
Good: Extend existing component with props/variants
Bad: Defining the same constant in multiple files
Good: Single source of truth, import everywhere
Abstract when:
Don't abstract when:
When you've made similar changes to multiple files:
Problem: When two different mechanisms must produce the same file set (e.g., recursive directory copy for init vs. manual files.set() for update), structural changes (renaming, moving, adding subdirectories) only propagate through the automatic mechanism. The manual one silently drifts.
Symptom: Init works perfectly, but update creates files at wrong paths or misses files entirely.
Prevention checklist: