refactor

A code-review assistant that finds common maintainability problems and links them to named ways of improving the code.

In plain words
What is it for?
Use it to find code smells, duplicate code, oversized methods or classes, complex conditions, and long parameter lists, then review suggested refactorings.
Why use it?
It helps locate hard-to-maintain code and suggests a specific improvement instead of requiring you to inspect the whole codebase manually.

Command

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/paulduvall/ai-development-patterns/refactor
Clone the repo
git clone --depth 1 https://github.com/PaulDuvall/ai-development-patterns
Per session 12 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 666 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.00012 $0.00666
Opus 5 $0.00006 $0.00333
Sonnet 5 $0.00002 $0.00133
Haiku 4.5 $0.00001 $0.00067

Measured 2d ago against content hash 2fbcc21bdfec, 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 2d 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.

examples/custom-commands/commands/refactor.md · 102 lines

How it starts

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

Refactoring Assistant

You are helping a developer improve code maintainability by identifying code smells and recommending specific refactoring techniques from Martin Fowler's catalog.

Usage Examples

/refactor              # Full analysis
/refactor --smell      # Code smells only
/refactor --duplicates # Find duplicate code
/refactor --suggest    # Quick suggestions without deep analysis

Implementation

1. Code Smell Detection

Analyze codebase for maintainability issues:

  • Long methods (>20 lines)
  • Duplicate code blocks (repeated patterns across files)
  • Complex conditionals (cyclomatic complexity >10)
  • Large classes (>250 lines)

For each smell found:

  • Identify exact location (file:line)
  • Assess severity (CRITICAL/HIGH/MEDIUM/LOW)
  • Suggest specific refactoring from Fowler's catalog
  • Estimate effort and risk

2. Bloater Detection

Find oversized code structures:

  • Methods with excessive parameters (>4)
  • Data clumps (same variables passed together repeatedly)
  • Primitive obsession (overuse of primitives vs objects)
  • Long parameter lists (>3-4 parameters)

3. Refactoring Strategy

For each issue identified:

  1. Name the code smell (e.g., "Long Method", "Feature Envy", "Shotgun Surgery")
  2. Recommend specific refactoring technique from Fowler's catalog:
    • Extract Method
    • Move Method
    • Replace Temp with Query
    • Introduce Parameter Object
    • Replace Conditional with Polymorphism
  3. Show before/after example with actual code snippets
  4. Estimate maintainability improvement (complexity reduction, testability increase)

4. Output Format

Generate step-by-step refactoring plan prioritized by impact:

## Refactoring Report

### Summary
- Total smells found: X
- Critical issues: Y
- Estimated total effort: Z hours

### Priority 1: Critical Issues
1. **Long Method in src/api/users.js:45-120**
   - Smell: Long Method (75 lines)
   - Severity: CRITICAL
   - Recommended refactoring: Extract Method
   - Estimated effort: 1 hour
   - Impact: Improves testability, reduces complexity by 40%

### Priority 2: High Issues
...

### Refactoring Plan
Step-by-step implementation guide with code examples

Read the full file on GitHub · 102 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. 2d ago First seen · 102 lines · 12 tokens per session scan A 2fbcc21bdfec

Subscribe to this mod's changes

refactor is a command published in the GitHub repository PaulDuvall/ai-development-patterns (645 stars, last pushed 3d ago), licensed MIT. It adds 12 tokens to every session and 666 once invoked, about $0.0001 per session on Opus 5. 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.