From xtras
Refactor code safely using clean code principles and design patterns. Use this skill whenever the user asks to refactor, clean up, simplify, restructure, modernize, or "improve" existing code; whenever they complain code is messy, hard to read, hard to test, duplicated, or "spaghetti"; whenever they ask which design pattern to apply; and during code review when suggesting structural improvements. Also use after writing new code
How this skill is triggered — by the user, by Claude, or both
Slash command
/xtras:clean-codeThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
This skill turns a refactoring request into a safe, disciplined process. Refactoring means changing the *structure* of code without changing its *behavior*. Most refactoring failures come from violating that definition — sneaking in behavior changes, taking steps too big to verify, or applying a pattern the code didn't need.
This skill turns a refactoring request into a safe, disciplined process. Refactoring means changing the structure of code without changing its behavior. Most refactoring failures come from violating that definition — sneaking in behavior changes, taking steps too big to verify, or applying a pattern the code didn't need.
Follow these phases in order. Do not skip phase 1 or 2 even under time pressure — they are what make the rest safe.
references/refactoring-catalog.md — the ones safe enough to do by inspection.Diagnose first, then apply. Full detection heuristics and worked remedies are in references/code-smells.md; step-by-step mechanics for each transformation are in references/refactoring-catalog.md.
| Smell | Telltale sign | Primary remedy |
|---|---|---|
| Long Method | Doesn't fit on a screen; comments marking "sections" | Extract Method per section; each comment is a candidate name |
| Large Class | Many unrelated fields/methods; "Manager"/"Util" name | Extract Class along responsibility seams |
| Duplicated Code | Same logic in 2+ places (even if not textually identical) | Extract Method/Function, then Pull Up if across siblings |
| Long Parameter List | 4+ params, or booleans steering behavior | Introduce Parameter Object; Replace Flag Argument with separate functions |
| Feature Envy | Method mostly reads another object's data | Move Method to the data it envies |
| Data Clumps | Same group of variables travels together | Extract Class/value object for the clump |
| Primitive Obsession | Strings/ints for domain concepts (money, IDs, emails) | Replace Primitive with value object |
| Switch/if-chain on type | Same conditional repeated in multiple places | Replace Conditional with Polymorphism (Strategy/State) — only if repeated |
| Shotgun Surgery | One change forces edits in many files | Move/Inline until the concept lives in one place |
| Divergent Change | One class changes for many unrelated reasons | Extract Class per reason-to-change (SRP) |
| Message Chains | a.getB().getC().getD() | Hide Delegate, or move behavior to the end of the chain |
| Speculative Generality | Unused hooks, params, abstract layers "for later" | Inline/delete; Collapse Hierarchy |
| Comments explaining what | Comment restates the code | Extract Method or Rename so the code says it; keep only why comments |
| Dead Code | Unreachable or unused members | Delete (version control remembers) |
| Mutable global/shared state | Hidden inputs, order-dependent behavior | Encapsulate; pass dependencies explicitly |
| Nested conditionals | Arrow-shaped code, deep indentation | Guard clauses; Decompose Conditional; early returns |
Patterns are remedies for recurring structural problems — not decoration. The most common failure mode of less experienced refactoring (human or model) is over-patterning: introducing Strategy, Factory, and Observer where a plain function and a dict would do. Rules of thumb:
When a pattern is genuinely warranted, read references/design-patterns.md for selection guidance, minimal implementations, and the smell each pattern answers.
For the fine-grained rules — naming, function size and shape, arguments, error handling, comments, formatting — read references/clean-code-principles.md. Apply these opportunistically during any refactor ("leave the campsite cleaner"), but keep such cleanups in their own small steps.
references/code-smells.md — full smell catalog: detection heuristics, examples, remedy mapping. Read when diagnosing in Phase 1.references/refactoring-catalog.md — mechanics of each named transformation (Extract Method, Move Method, Introduce Parameter Object, etc.) with safe step-by-step recipes. Read during Phase 3.references/design-patterns.md — pattern selection table, minimal modern implementations, anti-pattern warnings. Read only when the smell table points at a pattern.references/clean-code-principles.md — naming, functions, comments, error handling, tests, SOLID with practical caveats. Read when polishing or when the user asks about principles.Refactor what was asked plus the immediate campsite — not the whole codebase. If you notice larger problems (architecture, missing tests elsewhere), report them as recommendations rather than expanding the change. Large diffs are hard to review and hard to trust; several small reviewed refactors beat one heroic one.
Guides collaborative design exploration before implementation: explores context, asks clarifying questions, proposes approaches, and writes a design doc for user approval.
Creates structured, bite-sized implementation plans from specs or requirements before writing code. Useful for breaking down multi-step tasks into testable steps with file structure and task boundaries.
Resolves in-progress git merge or rebase conflicts by analyzing history, understanding intent, and preserving both changes where possible. Runs automated checks after resolution.
npx claudepluginhub thatxliner/claude-plugins --plugin xtras