Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Systematic refactoring with small-step discipline. Use when user says 'refactor', 'clean up', 'restructure', 'extract', 'rename', 'simplify', or mentions code smells. Enforces one change → test → commit cycle. For structural improvements, NOT style/formatting (use /lint). NOT for adding features or fixing bugs.
.claude/skills/aiskillstore-refactoring/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-13 | ✗→✓ | ▲ Improved | 34% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 0% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 622% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 383% | 0% |
| case-08 | ✗→✓ | ▲ Improved | 69% | 0% |
Improve code structure without changing behavior. One small step at a time.
Iron Law: ONE REFACTORING → TEST → COMMIT. Never batch changes.
Answer IN ORDER. Stop at first match:
Code smells (common triggers):
Is this actually refactoring?
| User Intent | Action | | ------------------- | ----------------------------------------------- | | "Make this cleaner" | ✓ Refactoring | | "Add validation" | ✗ New behavior → tdd-enforcer | | "Fix this bug" | ✗ Bug fix → tdd-enforcer or systematic-debugger | | "Format this code" | ✗ Style → /lint |
If not refactoring: Explain and suggest correct approach.
Does the code have tests?
| Coverage | Action | | ---------------- | --------------------------------------------- | | Well-tested | Skip to Phase 3 | | Partial coverage | Add characterization tests for untested parts | | No tests | Add characterization tests first |
Capture current behavior before refactoring:
typescript// Characterization test - captures ACTUAL behavior it('processOrder returns current behavior', () => { const result = processOrder({ items: [], user: null }); // Whatever it returns NOW is the expected value expect(result).toEqual({ status: 'empty', total: 0 }); });
Purpose: Safety net, not specification. Test what the code DOES, not what it SHOULD do.
Iron Law: ONE refactoring at a time. Run tests after EVERY change.
Tier 1 - Always Safe (no behavior change possible):
| Smell | Refactoring | Example | | -------------------- | -------------------- | -------------------------------------- | | Unclear name | Rename | d → discountAmount | | Long function | Extract Function | Pull 10 lines into calculateTax() | | Unnecessary variable | Inline Variable | Remove temp = x; return temp; | | Misplaced code | Move Function | Move validate() to Validator class |
typescript// ❌ Before: unclear name const d = price * 0.2; // ✅ After: Rename const discountAmount = price * 0.2;
Tier 2 - Safe with Tests (low risk if tests exist):
| Smell | Refactoring | Example | | ------------------- | ------------------------- | ------------------------------------------------- | | Repeated expression | Extract Variable | order.items.length > 0 → const hasItems = ... | | Complex conditional | Decompose Conditional | Extract if branches to named functions | | Nested conditionals | Guard Clauses | Early returns instead of deep nesting | | Magic literal | Replace Magic Literal | 0.2 → VIP_DISCOUNT_RATE | | Unused code | Remove Dead Code | Delete unreachable branches |
typescript// ❌ Before: nested conditionals function getDiscount(user) { if (user) { if (user.isVIP) { return 0.2; } else { return 0.1; } } return 0; } // ✅ After: Guard Clauses function getDiscount(user) { if (!user) return 0; if (user.isVIP) return 0.2; return 0.1; }
Tier 3 - Requires Care (higher risk, break into smaller steps):
| Smell | Refactoring | Caution | | -------------------------- | ------------------------------ | ------------------------------------------- | | God class | Extract Class | Do incrementally, move one method at a time | | Type-checking conditionals | Replace with Polymorphism | Requires class hierarchy | | Too many parameters | Introduce Parameter Object | Changes function signature | | Complex loop | Replace Loop with Pipeline | Ensure equivalent behavior |
Tie-breaker: If multiple refactorings apply, choose smallest scope first (Rename < Extract Variable < Extract Function < Extract Class).
After each refactoring:
refactor: [what changed]bashgit checkout -- <changed-files>
After revert:
STOP. Ask user:
> "I've attempted this refactoring twice and tests keep failing. This suggests either: > > 1. The refactoring is too large (need smaller steps) > 2. The code has hidden dependencies > 3. Tests are brittle > > How would you like to proceed?"
textMore refactoring needed? ├─ Yes → Return to Phase 3 (one more refactoring) └─ No → Done └─ Report: "Refactoring complete. Changes: [summary]"
Partial test coverage:
Refactoring reveals a bug:
User requests large refactoring:
| Don't | Do | | ------------------------------- | ------------------------------------- | | Batch multiple refactorings | One refactoring → test → commit | | "Fix" a failed refactoring | Revert, then try smaller step | | Refactor without tests | Add characterization tests first | | Change behavior during refactor | That's a feature/fix, not refactoring | | Skip the commit | Commit after every green test |
refactor: [description]| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-13 | fail→pass | 12,303 | 10,382 | -16% | 1 | 1 | 0% | 2,029 | 2,726 | +34% | 0 | 0 | — |
case-01 | fail→fail | 17,469 | 14,796 | -15% | 1 | 1 | 0% | 3,515 | 2,088 | -41% | 0 | 0 | — |
case-02 | fail→pass | 13,824 | 4,949 | -64% | 1 | 1 | 0% | 2,534 | 2,527 | -0% | 0 | 0 | — |
case-03 | fail→fail | 11,762 | 7,982 | -32% | 1 | 1 | 0% | 1,177 | 2,260 | +92% | 0 | 0 | — |
case-04 | fail→pass | 14,652 | 3,350 | -77% | 1 | 1 | 0% | 321 | 2,319 | +622% | 0 | 0 | — |
case-05 | fail→fail | 15,327 | 15,673 | +2% | 1 | 1 | 0% | 1,898 | 3,493 | +84% | 0 | 0 | — |
case-06 | fail→pass | 8,907 | 10,197 | +14% | 1 | 1 | 0% | 543 | 2,622 | +383% | 0 | 0 | — |
case-07 | pass→pass | 15,767 | 6,412 | -59% | 1 | 1 | 0% | 1,757 | 2,845 | +62% | 0 | 0 | — |
case-08 | fail→pass | 12,453 | 13,207 | +6% | 1 | 1 | 0% | 1,499 | 2,528 | +69% | 0 | 0 | — |
case-09 | pass→pass | 9,601 | 9,104 | -5% | 1 | 1 | 0% | 1,505 | 2,440 | +62% | 0 | 0 | — |
case-10 | pass→pass | 6,125 | 4,859 | -21% | 1 | 1 | 0% | 1,189 | 2,626 | +121% | 0 | 0 | — |
case-11 | pass→fail | 10,841 | 5,956 | -45% | 1 | 1 | 0% | 1,293 | 2,083 | +61% | 0 | 0 | — |
case-12 | fail→pass | 12,668 | 8,220 | -35% | 1 | 1 | 0% | 1,331 | 2,343 | +76% | 0 | 0 | — |
case-14 | pass→pass | 7,293 | 7,826 | +7% | 1 | 1 | 0% | 1,073 | 2,235 | +108% | 0 | 0 | — |
case-15 | pass→pass | 14,509 | 6,121 | -58% | 1 | 1 | 0% | 1,632 | 2,804 | +72% | 0 | 0 | — |
case-16 | fail→fail | 8,953 | 9,862 | +10% | 1 | 1 | 0% | 1,435 | 2,630 | +83% | 0 | 0 | — |
case-17 | pass→pass | 13,992 | 11,576 | -17% | 1 | 1 | 0% | 2,306 | 2,893 | +25% | 0 | 0 | — |
case-18 | fail→pass | 14,006 | 9,025 | -36% | 1 | 1 | 0% | 1,454 | 3,229 | +122% | 0 | 0 | — |
case-19 | fail→pass | 13,249 | 4,913 | -63% | 1 | 1 | 0% | 1,248 | 2,495 | +100% | 0 | 0 | — |
case-20 | fail→pass | 11,732 | 10,708 | -9% | 1 | 1 | 0% | 1,715 | 2,683 | +56% | 0 | 0 | — |
case-21 | fail→pass | 10,013 | 9,674 | -3% | 1 | 1 | 0% | 1,453 | 2,514 | +73% | 0 | 0 | — |
case-22 | pass→pass | 7,999 | 3,212 | -60% | 1 | 1 | 0% | 569 | 2,353 | +314% | 0 | 0 | — |
case-23 | pass→pass | 2,830 | 2,522 | -11% | 1 | 1 | 0% | 440 | 2,250 | +411% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 23 cases were attempted, and 22 counted toward the lift figure. The other 1 produced results that are not comparable between the two arms, so they are excluded from the headline rather than averaged into it. The headline lift of +39 percentage points is the difference between those two pass rates over the 22 comparable cases. 2 cases got worse with the skill loaded, and they are included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.