Claude Code Kit

Refactoring Guide

Provides Fowler-based refactoring recommendations with specific techniques, risk assessment, and step-by-step execution .

When to Use

Invoke with /refactoring-guide when:

  • Code smells have been identified and need systematic fixes
  • A module has grown unwieldy and needs restructuring
  • Preparing for a feature that requires cleaner foundations
  • After a /code-quality-audit to get actionable refactoring steps
  • Technical debt needs to be paid down incrementally

Process

Phase 1: Assess Current State

  1. Read the target code thoroughly — understand what it does before changing how it does it
  2. Identify existing tests — refactoring without tests is dangerous
  3. Map dependencies — what depends on this code? What does it depend on?
  4. Check for active development — avoid refactoring code with open PRs or in-progress features

Phase 2: Identify Refactoring Opportunities

Map code smells to Fowler's refactoring catalog:

Extract Techniques (breaking things apart)

SmellRefactoringWhen
Long functionExtract FunctionFunction >30 lines or does multiple things
Large classExtract ClassClass has multiple distinct responsibilities
Feature envyMove FunctionMethod uses another object's data more than its own
Data clumpsIntroduce Parameter ObjectSame group of parameters passed together
Primitive obsessionReplace Primitive with ObjectPrimitive carries domain meaning (email, money, ID)

Simplify Techniques (reducing complexity)

SmellRefactoringWhen
Nested conditionalsReplace Nested Conditional with Guard ClausesDeep nesting with early-exit cases
Switch/type codeReplace Conditional with PolymorphismSwitch on type that grows with each feature
Temp variablesReplace Temp with QueryTemp only assigned once and used in expression
Flag argumentsRemove Flag ArgumentBoolean parameter changes function behavior
Speculative generalityRemove Dead Code / Collapse HierarchyAbstractions never used by multiple implementations

Reorganize Techniques (improving structure)

SmellRefactoringWhen
Shotgun surgeryMove/Inline FunctionSingle change requires editing many classes
Divergent changeSplit Phase / Extract ClassOne class changed for multiple different reasons
Middle manRemove Middle ManClass delegates everything without adding value
Insider tradingEncapsulate Collection / Hide DelegateModules share too much internal knowledge

Phase 3: Risk Assessment

For each proposed refactoring:

  • Risk level: Low (rename, extract) / Medium (restructure, move) / High (change interface, split module)
  • Test coverage: Is the code covered? Can we add tests first?
  • Blast radius: How many files/modules are affected?
  • Reversibility: Can this be undone easily?
  • Incremental: Can this be done in small, individually-safe steps?

Phase 4: Execution Plan

For each approved refactoring, provide:

  1. Pre-conditions — tests to write or verify first
  2. Steps — atomic, individually-committable steps
  3. Verification — how to confirm each step didn't break anything
  4. Post-conditions — the expected state after completion

Output Format

# Refactoring Guide

## Target
[Module/file being refactored and why]

## Current Issues
1. [Smell] in [location] — [impact]

## Proposed Refactorings

### 1. [Refactoring Name] — [Target]
- **Technique**: [Fowler catalog name]
- **Risk**: Low/Medium/High
- **Blast radius**: N files
- **Pre-condition**: [Tests needed]

**Steps:**
1. [Atomic step]
2. [Atomic step]
3. Run tests → verify green

### 2. ...

## Execution Order
[Ordered list with dependencies noted]

## Safety Checklist
- [ ] All existing tests pass before starting
- [ ] Each step is committed separately
- [ ] Tests run after each step
- [ ] No behavior changes (refactoring only)
- [ ] Typecheck passes after each step

Notes

  • Refactoring means changing structure without changing behavior — if behavior changes, it's a rewrite
  • Always ensure test coverage before refactoring; if tests are missing, write them first
  • Prefer small, incremental refactorings over big-bang rewrites
  • Each refactoring step should be committable and deployable on its own

This skill is tactical — it applies Fowler's catalog to specific code smells. For complementary work at the architectural level:

  • /deepening-reviewTopological refactoring: identify shallow modules and turn them into deep ones. Different paradigm (depth/seams instead of smell/refactoring), different vocabulary (agent_docs/architecture-language.md). Use it before this skill when the friction is module-level rather than code-level.
  • /interface-design — When a refactoring will reshape an interface significantly, run this skill to compare alternative interface designs in parallel before committing.
  • /architecture-review — Structural SOLID assessment that often produces inputs for this skill.