| name | refactoring-patterns |
| description | Apply safe refactoring patterns to improve code structure without changing behavior. Use when cleaning up code, reducing technical debt, or improving maintainability. |
| category | development-practices |
| priority | medium |
| tokenEstimate | 1000 |
| agents | ["qe-code-reviewer","qe-quality-analyzer","qe-test-refactorer"] |
| implementation_status | optimized |
| optimization_version | 1 |
| last_optimized | "2025-12-03T00:00:00.000Z" |
| dependencies | [] |
| quick_reference_card | true |
| tags | ["refactoring","code-quality","technical-debt","maintainability","clean-code"] |
| trust_tier | 2 |
| validation | {"schema_path":"schemas/output.json","validator_path":"scripts/validate-config.json"} |
Refactoring Patterns
<default_to_action>
When refactoring:
- ENSURE tests pass (never refactor without tests)
- MAKE small change (one refactoring at a time)
- RUN tests (must stay green)
- COMMIT (save progress)
- REPEAT
Safe Refactoring Cycle:
npm test
npm test
git commit -m "refactor: extract calculateTotal"
Code Smells → Refactoring:
| Smell | Refactoring |
|---|
| Long method (>20 lines) | Extract Method |
| Large class | Extract Class |
| Long parameter list (>3) | Introduce Parameter Object |
| Duplicated code | Extract Method/Class |
| Complex conditional | Decompose Conditional |
| Magic numbers | Named Constants |
| Nested loops | Replace Loop with Pipeline |
NEVER REFACTOR:
- Without tests (write tests first)
- When deadline is tomorrow
- Code you don't understand
- Code that works and won't be touched
</default_to_action>
Quick Reference Card
Common Refactorings
| Pattern | Before | After |
|---|
| Extract Method | 50-line function | 5 small functions |
| Extract Class | Class doing 5 things | 5 single-purpose classes |
| Parameter Object | fn(a,b,c,d,e,f) | fn(options) |
| Replace Conditional | if (type === 'a') {...} | Polymorphism |