dev-loop-refactor-guide
Use when the user asks to refactor a long function or class safely, remove code smells, extract methods, untangle legacy code, or improve structure without…
by j4flmao★ 47 GitHub stars
Install dev-loop-refactor-guide in Claude Code & Claude Desktop
Sign up to install this skill
Create a free account to reveal the install command and save the skill to your library.
- Reveal the one-line install command
- Save skills to your synced library
- Get notified when skills update
About this skill
Refactoring Guide
Purpose
Systematically improve code structure, readability, maintainability, and extensibility without changing external behavior — applying proven refactoring techniques, identifying code smells, and establishing refactoring workflows that reduce technical debt safely.
Agent Protocol
Trigger
Exact user phrases: "refactor", "code smell", "technical debt", "improve code", "clean up code", "extract method", "restructure", "modernize", "legacy code", "refactoring strategy", "decouple", "extract class".
Input Context
- Codebase size and age (new project → legacy codebase)
- Language and framework (affects available refactoring tools)
- Current pain points (duplication, long methods, tight coupling, slow tests)
- Test coverage (% and quality)
- Refactoring goal (improve readability, enable new feature, reduce bugs, pay down debt)
- Risk tolerance (safety-critical, high-traffic, low-risk internal tool)
Output Artifact
Refactoring plan with prioritized code smells, technique application, and verification strategy.
Response Format
Target: {module/files}
Smells: {smell -> location}
Safety net: {existing tests | characterization tests to add}
Steps (each behavior-preserving, committed separately): {technique -> change}
Verification: {tests/commands}
Risk: {low|med|high} — rollback {plan}
Completion Criteria
- Code smells identified and cataloged
- Refactoring techniques selected for each smell
- Refactoring order prioritized (high value, low risk first)
- Test coverage verified for code being refactored
- Refactoring applied incrementally (one change at a time)
- Behavior preserved (tests pass before/after)
- Code review completed for refactoring PRs
- Documentation updated if API changed
Max Response Length
200 lines.
Framework/Methodology
Refactoring Decision Tree
What is the primary code quality issue?
├── Duplicated code → Extract method / Extract class
├── Long method (>20 lines) → Extract method / Replace temp with query
├── Large class (>300 lines) → Extract class / Extract interface
├── Too many parameters (>3) → Introduce parameter object
├── Feature envy → Move method / Extract class
├── Switch/if-else chain → Replace with strategy / polymorphism
├── Data clump → Introduce parameter object / Extract class
├── Shotgun surgery (one change = many files) → Move method / Inline class
├── Divergent change (file changes for multiple reasons) → Extract class per concern
├── Speculative generality → Inline class / Collapse hierarchy
├── Message chain (a.b().c().d()) → Hide delegate / Extract method
└── Primitive obsession → Replace primitive with value object
Refactoring Rule of Three
First time: Do it (get it working)
Second time: Duplicate it (get it working again)
Third time: Refactor it (eliminate duplication)
Don't refactor on first occurrence — you don't understand the pattern yet.
The Cambrian Explosion Trap
When refactoring, resist the urge to create the "perfect" abstraction immediately. Instead, make the simplest change that improves the code, then iterate.
Strategy:
1. Identify the smell
2. Apply the minimal refactoring technique
3. Run tests (verify behavior preserved)
4. Commit
5. Repeat
Workflow
Step 1: Identify Code Smells
// SMELL: Long method (>20 lines)
// SMELL: Deep nesting (arrow code)
function processOrder(order: Order, user: User, config: Config): Promise<Result> {
if (order && order.items && order.items.length > 0) {
if (user && user.isActive) {
if (config && config.enabled) {
let total = 0;
for (const item of order.items) {
if (item.price && item.quantity) {
total += item.price * item.quantity;
if (item.discount) {
total -= item.discount;
}
}
}
if (total > 0) {
if (user.balance >= total) {
user.balance -= total;
return saveOrder(order, user, total);
} else {
throw new Error('Insufficient balance');
}
} else {
throw new Error('Invalid total');
}
} else {
throw new Error('Config not enabled');
}
} else {
throw new Error('User not active');
}
} else {
throw new Error('Invalid order');
}
}
// SMELL: Primitive obsession (using string for concept)
function sendEmail(to: string, from: string, subject: string, body: string, cc?: string, bcc?: string, replyTo?: string) {
// ...
}
// SMELL: Feature envy (method uses more from other class)
class OrderService {
generateInvoice(order: Order): Invoice {
const invoice = new Invoice();
invoice.customerName = order.customer.name;
invoice.customerEmail = order.customer.email; // Uses Customer fields
invoice.customerAddress = order.customer.shippingAddress; // More Customer
invoice.items = order.items;
Allowed tools
Tools this skill is permitted to call.
No restriction — this skill can use any tool.
Bundled files
- references/migration-strategies.md
- references/refactor-guide-advanced.md
- references/refactor-guide-fundamentals.md
- references/refactor-patterns.md
- references/refactoring-patterns.md
- references/refactoring-techniques.md
FAQ
What does the dev-loop-refactor-guide skill do?
Use when the user asks to refactor a long function or class safely, remove code smells, extract methods, untangle legacy code, or improve structure without changing behavior. Produces a step-by-step refactoring plan with characterization tests. Not for tracking debt backlogs (use dev-loop-tech-debt-tracker) or system migrations (use enterprise-legacy-migration).
How do I install the dev-loop-refactor-guide skill?
Copy the skill folder into ~/.claude/skills (the Claude Code tab above does this in one command), or install it as a plugin.
Does the dev-loop-refactor-guide skill run scripts?
No, this skill is instructions only (SKILL.md) with no executable scripts.
Related skills
mcp-builder
AnthropicGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when…
claude-api
AnthropicReference for the Claude API / Anthropic SDK — model ids, pricing, params, streaming, tool use, MCP, agents, caching, token counting, model migration. TRIGGER…
algorithmic-art
AnthropicCreating algorithmic art using p5.js with seeded randomness and interactive parameter exploration. Use this when users request creating art using code…
mcp-integration
AnthropicThis skill should be used when the user asks to "add MCP server", "integrate MCP", "configure MCP in plugin", "use .mcp.json", "set up Model Context…