refactor

A code review tool that looks for common design and readability problems, then suggests or applies refactoring—reorganising code without changing its intended behaviour—with examples.

In plain words
What is it for?
Use it to review a file or directory, identify code smells such as overly complex functions or unclear names, and get example improvements or have the changes applied.
Why use it?
It helps find code that is too large, duplicated, tightly connected, deeply nested, or difficult to understand before those problems make changes riskier.

Command for Claude Code

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add commands/kid-sid/claude-spellbook/refactor
Clone the repo
git clone --depth 1 https://github.com/kid-sid/claude-spellbook

Made for: Claude Code.

Per session 0 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 691 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00691
Opus 5 $0.00000 $0.00345
Sonnet 5 $0.00000 $0.00138
Haiku 4.5 $0.00000 $0.00069

Measured 3d ago against content hash 2a0c04c82223, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

refactor scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 3d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.claude/commands/refactor.md · 101 lines

How it starts

The opening of the file, as written. The whole thing — 101 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Analyze code for smells and suggest or apply refactoring with examples.

Instructions

  1. Read the target file or directory the user specifies.
  2. Detect the language and analyze the code for these smell categories:

Code Smells to Detect

Size & Complexity:

  • Functions longer than 30 lines
  • Classes with more than 10 methods (God Class)
  • Cyclomatic complexity > 10
  • Deep nesting (> 3 levels)
  • Long parameter lists (> 4 parameters)

Duplication & Coupling:

  • Duplicate or near-duplicate code blocks
  • Shotgun surgery (one change requires editing many files)
  • Feature envy (method uses another class's data more than its own)
  • Inappropriate intimacy (classes that know too much about each other)

Naming & Clarity:

  • Unclear variable/function names
  • Magic numbers without named constants
  • Boolean parameters (use named options/enums instead)
  • Comments explaining "what" instead of code being self-documenting

Design:

  • Primitive obsession (using primitives instead of small value objects)
  • Data clumps (groups of data that always appear together)
  • Switch statements that should be polymorphism
  • Refused bequest (subclass doesn't use parent's methods)
  • Speculative generality (unused abstractions)

SOLID Violations:

  • Single Responsibility: class/function doing too many things
  • Open/Closed: modification required instead of extension
  • Liskov Substitution: subtypes not substitutable for base types
  • Interface Segregation: forced to depend on unused methods
  • Dependency Inversion: high-level modules depending on low-level details
  1. For each smell found, provide:

    • Location: file:line
    • Smell: name of the code smell
    • Severity: Low / Medium / High
    • Current code: the problematic snippet
    • Refactored code: the improved version
    • Technique: name of the refactoring pattern applied (e.g., Extract Method, Replace Conditional with Polymorphism, Introduce Parameter Object)
  2. Output format:

## Refactoring Report
**File(s):** {paths}
**Language:** {language}
**Issues found:** {count}

### High Priority

#### 1. {Smell Name} — {file:line}
**Technique:** {refactoring pattern}

Before:
```{lang}
{original code}

After:

{refactored code}

Why: {explanation of the improvement}


Medium Priority

{same format}

Low Priority

{same format}

Summary

  • {count} issues found across {severity levels}
  • Estimated improvement: {what gets better — readability, testability, maintainability}
  • Suggested order: {which refactors to do first}

Read the full file on GitHub · 101 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 3d ago First seen · 101 lines · 0 tokens per session scan A 2a0c04c82223

Subscribe to this mod's changes

refactor is a command published in the GitHub repository kid-sid/claude-spellbook (187 stars, last pushed 28d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 691 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.