/safeword:refactorfrom safeword
Guides step-by-step refactoring: one change, test, commit. Detects code smells (duplication, long functions, magic numbers, deep nesting, dead code, poor naming) and enforces Mikado ordering. Use for restructuring, simplifying, extracting, renaming.
Ships in safeword. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Improve code structure without changing behavior. One small step at a time.
Work one refactoring at a time: change, test, commit. Don't batch changes.
Use when the user says "refactor", "clean up", "restructure", "extract",
"rename", or "simplify", or when you've identified a code smell. Skip for adding
a feature or fixing a bug (use /bdd) and for formatting/style fixes (use
/lint).
Code smells (common triggers):
Scout first. Default for any multi-smell or unnamed request ("clean this up", "tidy this module"). Skip it only when the user named one specific smell — then go straight to Phase 3 with a one-entry ledger.
Discovery is the one phase that may fan out. Sweep in independent read-only passes — /lint + /audit for the mechanical smells (long function, deep nesting, magic literals, duplication, dead code — already detected there), plus separate judgment passes for the semantic ones (reuse / duplicated intent, wrong level of abstraction). Per smell category or per module these passes are independent, so run them concurrently (sub-agents where your harness supports it) and merge the findings — the same fan-out-then-merge shape as quality-review's independent reviewer passes. The model policy differs, though: these are discovery passes, not reviews of your own work, so quality-review's no-weaker reviewer rule does not apply — you (the orchestrator) verify the merged ledger. Spread the passes across smells/modules; cheaper or varied models are fine. Coverage diversity is the lever here, not model tier.
Merge into a ledger, not a loose list: each entry is a smell + location, ordered prioritized and dependency-aware — leaf-first, so no fix lands before the change it depends on (the Mikado ordering). Persist it as the Phase 5 checklist and never silently truncate — if you cap it, say what you dropped. A scout that reads as "nothing else" when it truncated is a bug.
Is this actually refactoring?
| User Intent | Action |
|---|---|
| "Make this cleaner" | ✓ Refactoring |
| "Add validation" | ✗ New behavior → /bdd |
| "Fix this bug" | ✗ Bug fix → /bdd or /debug |
| "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 |
When writing characterization tests: Characterization tests are still tests — apply behavioral testing principles (assert on what the system does, not how).
If the code can't be exercised under test (hard-wired dependencies, no instantiation point): introduce a seam first — the smallest dependency-break that lets a test run — then characterize. That seam is itself a Risk Level 3 refactoring; make it under whatever coverage exists, in its own step. (Feathers, Working Effectively with Legacy Code.)
Capture current behavior before refactoring:
// 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.
One refactoring at a time; run tests after every change.
Discovery fans out; mutation never does. The scout step (in "When to Use", above) may run parallel read-only passes. Execution is the opposite — one change, in isolation, then test, then commit — because the safety net lives here. Parallel or batched edits forfeit the revert protocol (Phase 4), break git bisect, and destroy single-change test attribution: you can no longer tell which edit turned the suite red. The one-change rule is per-edit, not per-session — you still work through every ledger entry (Phase 5), just one at a time.
Risk Level 1 - Lowest Risk (behavior-preserving when references are complete):
| 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 |
Not "behavior change impossible" — each has a failure mode when references or context are incomplete: Rename breaks exported/public-API names, string or reflection/serialized references, and tests that assert on the name; Inline Variable changes behavior if the variable captured a once-computed value but the expression is side-effecting or re-evaluated (e.g. inlining
const now = Date.now()used twice); Move Function can shiftthis/closure binding or introduce import-cycle / load-order effects (thevalidate()→ class example above changesthis). Verify all references before calling any of these done. Only Extract Function is near-unconditionally safe.
// ❌ Before: unclear name
const d = price * 0.2;
// ✅ After: Rename
const discountAmount = price * 0.2;
Risk Level 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 |
// ❌ 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;
}
Risk Level 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 |
| Wrong level of abstraction | Generalize the Mechanism (a design move, not one catalog refactoring — sequence it as Extract / Move / Inline steps, each its own edit) | Replace the special-case pile with one general path; pull the abstraction to the right altitude — don't add another branch |
Tie-breaker: If multiple refactorings apply, choose smallest scope first (Rename < Extract Variable < Extract Function < Extract Class).
After each refactoring:
refactor: [what changed] only when the commit can stay scoped.Before committing a green refactor, inspect git status --short --branch and
the files changed by this refactor.
Choose exactly one outcome:
refactor: [what changed].Deferring a commit is not skipping the one-change rule; it is preserving the same single-change attribution that rule exists to protect.
git checkout -- <changed-files>
After revert:
STOP. Ask user:
"I've attempted this refactoring twice and tests keep failing. This suggests either:
- The refactoring is too large (need smaller steps)
- The code has hidden dependencies
- Tests are brittle
How would you like to proceed?"
Walk the scout ledger leaf-first. Each entry gets its own Phase 3 → Phase 4 cycle; mark it done (or struck, with a reason) before starting the next. The ledger is the completeness contract — you stop when every entry is resolved or explicitly deferred, not when you're tired of the diff. Don't let an unfinished ledger read as "nothing left."
Ledger entries remaining?
├─ Yes → take next leaf-first entry → Phase 3 (one refactoring) → Phase 4 → mark done
└─ No → Done
├─ Run `/audit` to verify no dead code or new issues
└─ Report what was resolved and what was deferred (with reasons)
A single-named-smell request has a one-entry ledger — resolve it, audit, done.
Audit catches: Dead code left behind, new duplication (should decrease!), architecture violations.
Partial test coverage:
Refactoring reveals a bug:
/debug) or continue refactoringUser 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 |
| Create a mixed feature+refactor commit | Commit only an isolated refactor, or defer with the reason |
| Skip a safe commit | Commit after every green test when the commit can stay scoped |
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
24 skills in this plugin
/auditRun a diff-scoped code audit for architecture, dead code, and test quality. Uses the change from main to focus feature reviews; request a repository audit for whole-codebase discovery./bddBehavior-first feature development — use when building new capabilities, continuing feature work, or when work introduces new state or multiple user flows. Discovers desired behavior through examples and scenarios before implementation. Do NOT use for bug fixes, typos, or small isolated changes./brainstormUse when the user wants to explore options, weigh approaches, or think through uncertainty before committing to a direction. Collaborative brainstorming and rubber ducking — divergence-first thinking partner.npx claudepluginhub arcadeai/safeword --plugin safewordOther plugins' skills for the same job