Loading...
Loading...
Safe, phase-gated refactoring: CHARACTERIZE with tests, PLAN incremental steps, EXECUTE one change at a time, VALIDATE no regressions. Use when renaming functions/variables, extracting modules, changing signatures, restructuring directories, or consolidating duplicate code. Use for "refactor", "rename", "extract", "restructure", or "migrate pattern". Do NOT use for bug fixes or new feature implementation.
npx skill4agent add notque/claude-code-toolkit systematic-refactoring═══════════════════════════════════════════════════════════════
PHASE 1: CHARACTERIZE
═══════════════════════════════════════════════════════════════
Target Code:
- File: [path/to/file.ext]
- Element: [function/class/module being refactored]
- Lines: [start-end]
Current Behavior:
- Purpose: [what it does]
- Inputs: [parameters/dependencies]
- Outputs: [return values/side effects]
- Callers: [N files/functions use this]
Existing Tests:
- Coverage: [X% of target code]
- Test files: [list of relevant test files]
Characterization Tests Written:
- [ ] Happy path covered
- [ ] Edge cases covered
- [ ] Error cases covered
- [ ] Integration points covered
Test Execution:
$ [test command]
Result: ALL PASS (required to proceed)
CHARACTERIZE complete. Proceeding to PLAN...
══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
PHASE 2: PLAN
═══════════════════════════════════════════════════════════════
Refactoring Type: [rename | extract | restructure | migrate]
Target State:
- [Description of end state]
Incremental Steps:
1. [First atomic change]
- Files affected: [list]
- Risk: LOW/MEDIUM/HIGH
- Rollback: [how to undo]
2. [Second atomic change]
- Files affected: [list]
- Risk: LOW/MEDIUM/HIGH
- Rollback: [how to undo]
... [continue for all steps]
Dependencies:
Step 2 depends on: Step 1
Step 3 depends on: Step 2
...
Risks:
- [Potential issue 1]: [mitigation]
- [Potential issue 2]: [mitigation]
PLAN complete. Proceeding to EXECUTE...
══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
PHASE 3: EXECUTE - Step [N] of [Total]
═══════════════════════════════════════════════════════════════
Step [N]: [description]
Changes Made:
- [file1.ext]: [specific change]
- [file2.ext]: [specific change]
Test Execution:
$ [test command]
Result: PASS / FAIL
If PASS:
Step [N] complete
Commit: [commit message if applicable]
Proceeding to Step [N+1]...
If FAIL:
Step [N] failed
Rolling back...
Investigating failure before retry
══════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════════
PHASE 4: VALIDATE
═══════════════════════════════════════════════════════════════
Full Test Suite:
$ [comprehensive test command]
Result: [PASS/FAIL]
Behavior Verification:
- [ ] Original functionality preserved
- [ ] No new failures introduced
- [ ] Performance acceptable
Cleanup Verification:
- [ ] No orphaned code
- [ ] No unused imports
- [ ] No dead references
- [ ] Old names completely removed
Caller Verification:
$ grep -r "[old_name]" --include="*.ext"
Result: [No matches / N remaining references]
Documentation:
- [ ] Comments updated
- [ ] README updated (if applicable)
- [ ] API docs updated (if applicable)
Final Status: COMPLETE / INCOMPLETE
Summary:
- Files changed: [N]
- Lines changed: [+X/-Y]
- Steps completed: [N/Total]
- Tests: [X passed, Y total]
═══════════════════════════════════════════════════════════════Phase 1: Find all usages with Grep
Phase 2: Plan order (definition first, then callers)
Phase 3: Execute with replace_all where safe
Phase 4: Verify no old name references remainPhase 1: Identify code to extract, write tests
Phase 2: Plan new location, interface design
Phase 3:
- Step 1: Create new location with copy
- Step 2: Update callers one by one
- Step 3: Remove old code
Phase 4: Verify all callers use new locationPhase 1: Find all usages, understand all variations
Phase 2: Plan inline order (start with simplest callers)
Phase 3:
- Step 1: Inline at first call site
- Step 2: Repeat for each call site
- Step 3: Remove now-unused function
Phase 4: Verify no remaining referencesPhase 1: Find all callers, understand usage patterns
Phase 2: Plan migration (add new, migrate, remove old)
Phase 3:
- Step 1: Add new signature alongside old
- Step 2: Migrate callers one by one
- Step 3: Remove old signature
Phase 4: Verify all callers use new signatureUser: "Rename getUserData to fetchUserProfile across the entire codebase"
Claude: *Changes 47 files in one commit, updating function name, all callers, tests, and docs*User: "Extract this logic into a new function"
Claude: *Immediately creates new function and updates callers without writing tests*User: "Move getUser from utils.js to user-service.js"
Claude: *Creates new location, updates 80% of callers, leaves old function "for backward compatibility"*User: "Rename calculateTotal and also fix the tax calculation bug"
Claude: *Renames function AND changes logic in same refactoring*| Rationalization | Why It's Wrong | Required Action |
|---|---|---|
| "This refactoring is safe, no tests needed" | Refactoring without tests is flying blind | Write characterization tests first |
| "I'll update the remaining callers later" | Incomplete migrations rot forever | Migrate ALL callers before removing old code |
| "Small rename, no need for full process" | Small renames break string refs and configs | Grep for all references including strings |
| "I can fix this bug while refactoring" | Mixed concerns make failures undiagnosable | Separate commits: refactor then fix |