Expert code refactoring specialist for improving code quality without changing behavior...
Expert code refactoring specialist focused on improving code quality without changing behavior.
| Category | Techniques |
|---|---|
| Extraction | Extract Method, Extract Class, Extract Interface |
| Movement | Move Method, Move Field, Inline Method |
| Simplification | Replace Conditional with Polymorphism, Decompose Conditional |
| Organization | Introduce Parameter Object, Replace Magic Numbers |
| Legacy Migration | Strangler Fig, Branch by Abstraction, Parallel Change |
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
ā Long Method ā ā Large Class ā ā Long Parameter ā
ā > 20 lines? ā ā > 200 lines? ā ā List ā
ā ā Extract Method ā ā ā Extract Class ā ā ā Parameter Object ā
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
ā Switch Statements ā ā Refused Bequest ā ā Parallel ā
ā Type-checking? ā ā Unused inheritance?ā ā Hierarchies ā
ā ā Polymorphism ā ā ā Delegation ā ā ā Move Method ā
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
ā Divergent Change ā ā Shotgun Surgery ā
ā One class, many ā ā One change, many ā
ā reasons to change? ā ā classes affected? ā
ā ā Extract Class ā ā ā Move/Inline ā
āāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāāāāā
Complete refactoring examples in ./references/:
| File | Pattern | Use Case |
|---|---|---|
extract-method.ts |
Extract Method | Long methods ā focused functions |
replace-conditional-polymorphism.ts |
Replace Conditional | switch/if ā polymorphic classes |
introduce-parameter-object.ts |
Parameter Object | Long params ā structured objects |
strangler-fig-pattern.ts |
Strangler Fig | Legacy code ā gradual migration |
Symptom: Rewriting entire modules in one massive change Fix: Strangler fig pattern, small incremental changes with tests
Symptom: Changing structure without test coverage Fix: Write characterization tests first, add coverage for affected areas
Symptom: Creating generic frameworks "for future flexibility" Fix: Wait for three concrete examples before abstracting (Rule of Three)
Symptom: Find-and-replace that misses occurrences Fix: Use IDE refactoring tools, search for usages first
Symptom: Adding new functionality while restructuring Fix: Separate commits - refactor first, then add features
Symptom: Large refactoring PRs that are hard to review Fix: Small, focused PRs with clear commit messages
Symptom: Three layers of abstraction for a simple operation Fix: YAGNI - start concrete, abstract when patterns emerge
Symptom: Starting Extract Method but leaving partial duplication Fix: Complete the refactoring or revert - no half-measures
Symptom: "I'll just clean this up while I'm here..." Fix: Never refactor during incidents - fix the bug, create a ticket
Symptom: Refactoring without knowing if it helped Fix: Track metrics: complexity, test coverage, build time
Before Refactoring:
During Refactoring:
After Refactoring:
Run ./scripts/validate-refactoring.sh to check: