| description | Safe refactoring with automated review, testing, and rollback capabilities |
|---|---|
| argument-hint | Optional flags (--auto for automated mode) |
You are helping a developer perform safe refactoring with comprehensive safety checks. Combine built-in review commands with custom safety workflows: baseline analysis, isolated branching, full test execution, performance comparison, and automated quality gates.
/safe-refactor # Interactive refactoring with safety checks
/safe-refactor --auto # Automatic refactoring with approval gatesRun built-in review command to analyze current code state:
/reviewCapture baseline metrics:
- Current test coverage percentage
- Cyclomatic complexity scores
- Performance benchmarks (if available)
- Code quality metrics
Create isolated git branch for refactoring:
git checkout -b refactor-$(date +%Y%m%d-%H%M%S)Branch naming convention:
refactor-YYYYMMDD-HHMMSSfor timestamp-based tracking- Example:
refactor-20250110-143022
Execute refactoring based on analysis:
- Apply specific refactoring patterns (Extract Method, Introduce Parameter Object, etc.)
- Maintain existing functionality (behavior-preserving changes only)
- Update related tests if necessary
- Add inline comments explaining complex refactoring decisions
Verify all existing tests still pass:
# Run full test suite
npm test # or pytest, go test, etc.Requirements:
- 100% of existing tests must pass
- No test modifications allowed unless absolutely necessary
- If tests fail, explain failure and provide fix options
Create additional tests for refactored code:
- Test new methods created during refactoring
- Verify edge cases for extracted functions
- Add integration tests if refactoring spans multiple modules
- Ensure code coverage remains at or above baseline
Run performance benchmarks (if applicable):
# Example for Node.js
npm run benchmark
# Example for Python
python -m pytest tests/benchmarks/ --benchmark-onlyAcceptable outcomes:
- Performance improvement (ideal)
- No significant performance change (acceptable)
- Performance regression <5% (requires justification)
- Performance regression >5% (requires approval or rollback)
Create comprehensive report:
## Refactoring Report
**Branch**: refactor-20250110-143022
**Date**: 2025-01-10 14:30:22
**Status**: READY FOR REVIEW / NEEDS ATTENTION
### Changes Summary
- Files modified: X
- Lines added: Y
- Lines removed: Z
- Net change: +/-N lines
### Refactoring Patterns Applied
1. Extract Method (src/api.js:45 → extractUserValidation())
2. Introduce Parameter Object (src/checkout.js:120)
3. Replace Conditional with Polymorphism (src/payment.js:78)
### Test Results
- Existing tests: ✅ 287/287 passed
- New tests added: 12
- Code coverage: 85% (baseline: 83%)
### Performance Comparison
| Metric | Before | After | Change |
|--------|--------|-------|--------|
| API Response Time | 245ms | 198ms | -19% ✅ |
| Memory Usage | 128MB | 125MB | -2% ✅ |
| Test Suite Duration | 12.5s | 12.3s | -1.6% ✅ |
### Quality Metrics
| Metric | Before | After | Change |
|--------|--------|-------|--------|
| Cyclomatic Complexity | 8.2 | 5.4 | -34% ✅ |
| Lines per Method | 24 | 15 | -37% ✅ |
| Method Count | 45 | 52 | +16% |
### Risks
- None identified
### Recommendation
✅ Safe to merge - All quality gates passedGenerate pull request with refactoring summary:
gh pr create --title "Refactor: [Description]" --body "$(cat refactoring-report.md)"Include in PR description:
- Refactoring patterns applied
- Test results and coverage
- Performance impact
- Rollback plan
If refactoring introduces issues:
# Return to main branch
git checkout main
# Delete refactoring branch
git branch -D refactor-YYYYMMDD-HHMMSSRollback criteria:
- Test failures that cannot be quickly resolved
- Performance regression >5%
- Unexpected behavior in production-like environment
- Team vote to abandon refactoring
Automatic approval if ALL conditions met:
- ✅ All existing tests pass
- ✅ Code coverage maintained or improved
- ✅ No performance regression >5%
- ✅ Cyclomatic complexity reduced or unchanged
- ✅ No new security issues
Manual review required if ANY condition fails.
Parse $ARGUMENTS to determine refactoring mode:
--auto: Automatic mode with approval gates (proceed if all safety gates pass)- No arguments: Interactive mode (ask for user approval at each step)
- Always create a safety branch
- Never modify existing test behavior without justification
- Run full test suite, not just affected tests
- Compare performance metrics before/after
- Document all refactoring patterns applied
- Keep refactoring PRs focused (single responsibility)
- Have clear rollback plan before starting
- Use built-in
/reviewcommand for baseline analysis