Systematic technical debt reduction and code refactoring to improve maintainability and reduce complexity
Refactors code systematically to reduce technical debt, improve maintainability, and modernize while preserving behavior through test-driven incremental changes.
/plugin marketplace add avovello/cc-plugins/plugin install refactor@cc-pluginsPurpose: Systematic technical debt reduction and code refactoring to improve maintainability, reduce complexity, and modernize code
The refactor command addresses the critical gap between identifying technical debt (via Audit plugin) and actually fixing it. Research shows teams spend 10-30% of sprint time on refactoring, making this a critical workflow.
Key Distinction: Refactor improves existing code without changing behavior, while Feature adds new functionality and Bugfix repairs broken behavior.
Check target exists
Determine refactoring scope
Identify project type
Run pre-refactor-analyzer agent to:
Output:
refactor-plan/
├── CURRENT_STATE.md # Current code analysis
├── COMPLEXITY_METRICS.md # Before metrics
├── EXISTING_TESTS.md # Current test coverage
└── OPPORTUNITIES.md # Refactoring opportunities
Run debt-prioritizer agent to:
Output: Prioritized list with recommendations
Run refactor-planner agent to:
Output: REFACTORING_PLAN.md with step-by-step approach
Present plan to user:
Wait for approval before proceeding.
Based on refactoring type, launch appropriate agents in parallel:
Always Launch:
Conditionally Launch (based on refactoring type):
Code Structure:
Modernization:
Performance:
Architecture:
For each refactoring step:
Apply Change
Run Tests (test-preserver agent)
If Tests Pass: ✅
If Tests Fail: ❌
Measure Progress
Run refactor-validator agent to:
Success Criteria:
Generate documentation:
refactor-output/
├── REFACTORING_SUMMARY.md # What was changed and why
├── IMPROVEMENTS.md # Before/after metrics
├── COMMIT_MESSAGE.md # Suggested commit message
└── TECHNICAL_DEBT_UPDATE.md # Updated tech debt status
If requested, create git commit:
For each refactoring step:
Apply → Test → (Pass?) → Commit → Next
↓ Fail
Revert or Fix → Re-test → (Pass?) → Commit
↓ Fail (after 2 attempts)
Skip & Log
Max Iterations: 50 refactoring steps per session Safety: Each step is tested before proceeding
Optional .claude/refactor-config.yaml:
refactor:
safety:
require_test_coverage: true # Require tests before refactoring
min_coverage_threshold: 80 # Min coverage % required
auto_revert_on_failure: true # Auto revert if tests fail
max_attempts_per_step: 2 # Max attempts to fix failing tests
scope:
max_steps_per_session: 50 # Max refactoring steps
incremental_commits: true # Commit after each successful step
priorities:
prefer_quick_wins: true # Prioritize high impact / low effort
max_risk_level: "medium" # Skip high-risk refactorings
metrics:
track_complexity: true # Track cyclomatic complexity
track_duplication: true # Track code duplication
track_coverage: true # Track test coverage
/refactor "reduce complexity of src/auth/validateUser.js"
# Output:
# ✅ Analyzed validateUser function (complexity: 15)
# ✅ Planned 5 refactoring steps
# ✅ Step 1/5: Extract email validation → tests pass
# ✅ Step 2/5: Extract password validation → tests pass
# ✅ Step 3/5: Extract role checking → tests pass
# ✅ Step 4/5: Simplify conditionals → tests pass
# ✅ Step 5/5: Improve variable names → tests pass
#
# Complexity reduced: 15 → 6 (60% improvement)
# All 23 tests pass ✅
/refactor "eliminate duplication in src/services/"
# Output:
# ✅ Found 8 instances of duplicate code
# ✅ Prioritized by impact (420 lines can be eliminated)
# ✅ Step 1/8: Extract common validation logic → tests pass
# ✅ Step 2/8: Create shared utility function → tests pass
# ...
#
# Duplication eliminated: 420 lines → 65 lines (85% reduction)
# All 156 tests pass ✅
/refactor "update outdated dependencies"
# Output:
# ✅ Analyzed 45 dependencies
# ✅ Found 12 outdated (8 with breaking changes)
# ✅ Prioritized by risk
# ✅ Step 1/12: Update lodash 4.17.20 → 4.17.21 → tests pass
# ✅ Step 2/12: Update axios 0.27.0 → 1.6.0 → tests pass
# ⚠️ Step 3/12: Update webpack 4 → 5 → tests fail
# → Analyzing failures...
# → Fixed config incompatibility
# → Re-tested → tests pass
# ...
#
# Updated 12 dependencies
# All 203 tests pass ✅
/refactor "modernize src/utils/ to ES6+"
# Output:
# ✅ Analyzed code (ES5 patterns detected)
# ✅ Planned modernization steps
# ✅ Step 1/15: Convert var → const/let → tests pass
# ✅ Step 2/15: Convert function() → arrow functions → tests pass
# ✅ Step 3/15: Use destructuring → tests pass
# ✅ Step 4/15: Use template literals → tests pass
# ✅ Step 5/15: Use async/await instead of promises → tests pass
# ...
#
# Modernized 45 files to ES6+
# All 178 tests pass ✅
# After running /audit, you have TECHNICAL_DEBT.md with issues
/refactor "address high-priority items from audit"
# Output:
# ✅ Loaded audit-output/TECHNICAL_DEBT.md
# ✅ Found 5 high-priority items
# ✅ Prioritized by impact/effort
#
# Item 1/5: God class UserService (650 lines)
# ✅ Split into UserAuthService
# ✅ Split into UserProfileService
# ✅ Split into UserNotificationService
# → tests pass
#
# Item 2/5: Long function processPayment (120 lines)
# ✅ Extract validation logic
# ✅ Extract payment processing
# ✅ Extract notification logic
# → tests pass
# ...
Before completing refactoring:
If any gate fails:
refactor-output/
├── CURRENT_STATE.md # Initial analysis
├── REFACTORING_PLAN.md # Step-by-step plan
├── REFACTORING_SUMMARY.md # What changed
├── IMPROVEMENTS.md # Before/after metrics
├── COMPLEXITY_BEFORE.md # Initial metrics
├── COMPLEXITY_AFTER.md # Final metrics
├── COMMIT_MESSAGE.md # Suggested commit message
└── TECHNICAL_DEBT_UPDATE.md # Updated debt status
/audit identifies debt → /refactor fixes itThe plugin supports these common refactorings:
Method-Level:
Class-Level:
Code Cleanup:
Modernization: