Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Perform safe refactoring operations while preserving behavior and maintaining code integrity. Use when: renaming identifiers, extracting functions/modules, inlining code, moving files, or restructuring code without changing functionality. Do NOT use when: adding new features, fixing bugs (use standard editing), or making breaking API changes.
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-07 | ✗→✓ | ▲ Improved | 56% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 55% | 0% |
| case-14 | ✗→✓ | ▲ Improved | 55% | 0% |
| case-17 | ✗→✓ | ▲ Improved | 67% | 0% |
| case-21 | ✗→✓ | ▲ Improved | 64% | 0% |
Execute safe refactoring operations with verification at each step. Refactoring changes code structure without altering external behavior.
Preserve behavior. Every refactoring must pass tests before and after. If behavior changes, it's not refactoring—it's a rewrite.
Rename variables, functions, classes, files, or directories while maintaining all references.
When to use:
Steps:
bash# Find all usages before renaming grep -r "oldName" --include="*.ts" --include="*.js" --include="*.py"
Pull out a code block into a new function, method, class, or module.
When to use:
Steps:
Extraction targets:
Replace a function call with the function body.
When to use:
Steps:
Relocate code to a more appropriate location in the codebase.
When to use:
Steps:
Before any refactoring, verify:
bash# 1. Ensure clean git state git status git stash # or commit work-in-progress # 2. Run existing tests to establish baseline # Pick the appropriate test command for your project npm test 2>/dev/null || go test ./... 2>/dev/null || pytest 2>/dev/null || cargo test 2>/dev/null # 3. Create a checkpoint git checkout -b refactor/<description>
Pre-refactoring checklist:
bash # Ensure tests exist # If no tests, consider adding characterization tests first
# Create refactoring branch git checkout -b refactor/<description>
bash # Run tests npm test 2>/dev/null || go test ./...
# Verify build succeeds npm run build 2>/dev/null || go build ./...
bash git add -A git commit -m "ref: <what changed>"
bash # Comprehensive verification npm test 2>/dev/null && npm run lint 2>/dev/null
bash # Remove any dead code # Update documentation if needed # Merge or create PR
typescript// Before function processOrder(order: Order) { // 50 lines of validation // 30 lines of calculation // 20 lines of persistence } // After function processOrder(order: Order) { const validated = validateOrder(order); const calculated = calculateTotals(validated); return persistOrder(calculated); }
bash# Before: utils.js has 500 lines # After: Split into organized modules mkdir -p src/utils/validation mkdir -p src/utils/formatting # Move validation functions mv src/utils/validateUser.js src/utils/validation/ mv src/utils/validateOrder.js src/utils/validation/ # Update imports find . -name "*.ts" -exec sed -i 's|from.*utils/validateUser|from../utils/validation/validateUser|g' {} \;
bash# For large codebases, use language server # TypeScript npx tsserver --rename <file> <oldName> <newName> # Or use find + sed for simpler cases grep -rl "oldFunctionName" src/ | xargs sed -i 's/oldFunctionName/newFunctionName/g'
When moving code creates circular imports:
If refactoring must change public API:
For codebase-wide changes:
bash# 1. Check what changed git diff HEAD~1 # 2. Verify test expectations # Look for tests that assert implementation details # 3. Consider if test needs updating (rare) # Only if refactoring exposed test as brittle
bash# TypeScript: Check for missing imports npx tsc --noEmit # Go: Check for unused imports goimports -l . # General: Search for references to old names grep -r "oldName" --include="*.ts" --include="*.js" --include="*.go"
bash# Profile before and after # Compare key metrics # Ensure refactoring didn't introduce N+1 queries or unnecessary copies
Other measured skills in the registry, with their headline benchmark lift.