Disciplined refactoring with analysis-first approach and incremental changes
Provides a disciplined approach to refactoring code with analysis-first methodology. Ensures safe transformations through incremental changes with verification at each step.
Core Principle: Refactor without changing behavior. Tests must pass before and after.
Identify code smells and prioritize high-impact refactorings:
Apply 80/20 principle - focus on least changes with most impact:
Ensure safe transformations with verification at each step:
Make incremental changes with git commits:
| Principle | Description |
|---|---|
| Analysis-first | Understand before changing |
| Preserve behavior | Don't change functionality |
| Small steps | One transformation at a time |
| Verify | Run tests after each change |
| Commit frequently | Easy to rollback |
| Smell | Detection | Refactoring |
|---|---|---|
| God Class | > 300 lines | Extract Class |
| God Function | > 50 lines | Extract Method |
| Deep Nesting | > 4 levels | Extract Method, Guard Clauses |
| Long Parameter List | > 4 parameters | Introduce Parameter Object |
| Smell | Detection | Refactoring |
|---|---|---|
| Feature Envy | Method uses another class more | Move Method |
| Inappropriate Intimacy | Accessing other class internals | Hide Delegate |
| Message Chains | Long chains of calls | Hide Delegate |
| Smell | Detection | Refactoring |
|---|---|---|
| Missing Abstraction | Complex logic without class | Extract Class |
| Primitive Obsession | Overuse of primitives | Introduce Value Object |
| Duplicate Code | Similar code blocks | Extract Method |
Identify code smells and measure complexity:
1. Run code-analyzer skill
2. Identify code smells
3. Measure complexity metrics
4. Prioritize by impact
Prioritize refactorings by impact (80/20):
1. List all refactorings
2. Estimate effort for each
3. Estimate benefit for each
4. Sort by benefit/effort ratio
5. Select top refactorings
One small change at a time:
FOR each refactoring:
RUN tests (must pass)
MAKE one change
RUN tests (must pass)
COMMIT with message
Run tests after each change:
IF tests fail:
ROLLBACK change
ANALYZE failure
TRY different approach
Commit after each successful refactor:
git commit -m "refactor: [description of refactoring]"
When to use: Long methods, duplicated code
Steps:
When to use: God classes, multiple responsibilities
Steps:
When to use: Unclear names
Steps:
When to use: Feature envy
Steps:
When to use: Complex conditionals
Steps:
### 🔧 Refactor Session: AuthService
**Analysis:**
- Complexity: 15 (threshold: 10)
- Lines: 450 (threshold: 300)
- Smells: God Class, Long Method
**Plan:**
1. Extract TokenService from AuthService (high impact, medium effort)
2. Extract ValidationHelper (medium impact, low effort)
3. Split handleOAuth method (high impact, low effort)
**Execution:**
#### Refactor 1: Extract TokenService
- Tests before: ✅ 24/24 passing
- Change: Extract token methods to new TokenService class
- Tests after: ✅ 24/24 passing
- Commit: `refactor: extract TokenService from AuthService`
#### Refactor 2: Extract ValidationHelper
- Tests before: ✅ 24/24 passing
- Change: Extract validation methods to ValidationHelper
- Tests after: ✅ 24/24 passing
- Commit: `refactor: extract ValidationHelper from AuthService`
#### Refactor 3: Split handleOAuth
- Tests before: ✅ 24/24 passing
- Change: Split into handleOAuthSuccess, handleOAuthError
- Tests after: ✅ 24/24 passing
- Commit: `refactor: split handleOAuth into smaller methods`
**Result:**
- Complexity: 8 (was 15)
- Lines: 180 (was 450)
- Smells: None
Used by:
Related Skills:
code-analyzer: For identifying code smellstdd-workflow: For ensuring tests pass after refactoringBefore each refactoring:
After each refactoring: