Find Duplicate Code
- Look for structural duplication:
# Similar function signatures
grep -rn "async function\|const .* = async" ${1:-src}/ --include="*.ts" | sort | head -40
# Similar patterns (fetch calls, error handling, etc.)
grep -rn "try {" ${1:-src}/ --include="*.ts" -l
- Read suspected duplicate files side by side.
- Identify duplication categories:
- Identical blocks: copy-pasted code
- Near-duplicates: same logic, different variable names
- Structural similarity: same pattern, different implementations
- For each duplication cluster:
- Show the duplicated sections
- Propose a shared abstraction (utility function, custom hook, base class)
- Show how call sites would look after extraction
- Ask which ones to consolidate, then apply.
Focus on impactful duplications (3+ occurrences or > 20 lines).