Refactoring Guide
Refactoring Principles
-
Tests first - Never refactor without test coverage
-
Small steps - One change at a time, commit frequently
-
Behavior unchanged - Refactoring ≠ feature change
-
Clear purpose - Know WHY you're refactoring
When to Refactor
Good reasons:
-
Adding a feature is harder than it should be
-
Bug fixes keep touching the same code
-
Code is hard to understand after a break
-
Duplicate code across multiple places
-
Performance requires structural change
Bad reasons:
-
"I don't like how it looks"
-
"I would have done it differently"
-
"Let's modernize everything"
Code Smells & Fixes
Structural Smells
Smell Symptom Refactoring
Long Function
30 lines, multiple responsibilities Extract Method
Large Class
300 lines, many concerns Extract Class
Long Parameter List
4 params Introduce Parameter Object
Feature Envy Method uses another class more than its own Move Method
Primitive Obsession Strings/numbers representing concepts Value Object
Code Duplication
Type Fix
Identical code Extract to shared function
Similar code Extract with parameters
Parallel inheritance Template Method pattern
Coupling Issues
Smell Fix
Inappropriate Intimacy Move methods, introduce interface
Message Chains Hide Delegate
Middle Man Remove unnecessary delegation
Safe Refactoring Steps
Extract Function
- Identify code to extract
- Create new function with clear name
- Copy code to new function
- Replace original with function call
- Run tests
- Commit
Rename
- Use IDE rename (not find-replace)
- Update all references automatically
- Check for string references manually
- Run tests
- Commit
Move Code
- Copy to new location
- Make old location delegate to new
- Run tests
- Update callers to use new location
- Remove delegation
- Run tests
- Commit
Refactoring Strategies
Strangler Fig Pattern:
-
Build new alongside old
-
Route traffic gradually
-
Remove old when unused
Branch by Abstraction:
-
Introduce abstraction layer
-
Implement new version behind it
-
Switch implementations
-
Remove abstraction if desired
Parallel Change:
-
Add new, don't modify old
-
Migrate callers incrementally
-
Remove old when unused
Red Flags During Refactoring
Stop and reassess if:
-
Tests are failing
-
Scope keeps growing
-
You're fixing bugs while refactoring
-
Changes cascade unexpectedly
-
You can't explain the improvement
Output Format
When proposing refactoring:
-
Current Issues - Specific problems with current code
-
Proposed Changes - What will change and why
-
Migration Path - Step-by-step safe transition
-
Risk Assessment - What could go wrong
-
Verification - How to confirm success