refactor
Analyze code and suggest concrete refactorings with before/after examples.
When & Why to Use This Skill
This Claude skill is a specialized tool for enhancing code quality and maintainability through automated analysis and structured refactoring. It identifies technical debt—such as code smells, excessive complexity, and naming inconsistencies—and provides concrete, actionable before-and-after examples. By utilizing a comprehensive refactoring checklist and the ability to spawn designer subagents for large-scale architectural changes, it ensures that codebases remain clean, modular, and scalable.
Use Cases
- Technical Debt Reduction: Identifying and fixing common code smells like duplication, 'magic numbers', and primitive obsession in legacy codebases.
- Modularization: Decomposing 'God objects' or overly long functions (40+ lines) into smaller, reusable helper functions and modules.
- Readability Enhancement: Improving code clarity by renaming variables with intent, reducing nesting depth through early returns, and aligning styles with the existing codebase.
- Architectural Restructuring: Planning and executing complex changes that span multiple files, such as splitting modules or changing class hierarchies using the designer subagent.
- Pre-merge Optimization: Automatically reviewing code changes before a pull request to ensure they meet size, structure, and naming standards.
| name | refactor |
|---|---|
| description | Analyze code and suggest concrete refactorings with before/after examples. |
| allowed-tools | Read, Grep, Glob, Bash, Task |
Refactor
Analyze code and suggest concrete refactorings. For large architectural changes, spawn designer subagent.
Workflow
- Identify scope - file, function, or module
- Analyze - check against refactoring checklist
- For large refactors - spawn
designersubagent for architectural planning - Propose changes - specific before/after with priority order
- Offer to implement - if user approves
When to Use Designer Subagent
Spawn designer via Task tool when:
- Splitting a module into multiple files
- Changing class/type hierarchy
- Restructuring dependencies
- Anything touching 3+ files
For single-file refactors, analyze directly.
Refactoring Checklist
Naming
| Issue | Action |
|---|---|
| Unclear names | Rename with intent |
_v2, _new suffixes |
Replace with descriptive |
| Magic numbers | Extract to constants |
| Inconsistent style | Align with codebase |
Size
| Metric | Threshold | Action |
|---|---|---|
| Function lines | >40 | Extract helpers |
| Parameters | >4 | Parameter object |
| Nesting depth | >3 | Early return / extract |
| File lines | >400 | Split module |
Smells
| Smell | Action |
|---|---|
| Duplication (3+ lines, 2+ times) | Extract function |
| Feature envy | Move method |
| Long param list | Parameter object |
| Primitive obsession | Value types |
| Dead code | Delete |
| Speculative generality | Delete |
Structure
- Multiple responsibilities → Split
- Deep nesting → Early returns
- God object → Decompose
Output Format
## Refactoring: [Name]
Location: file:line
Problem: [Why it matters]
Before:
[code]
After:
[code]
Benefit: [Improvement]
Summary
## Refactoring Plan
Priority:
1. [Quick win] - file:line
2. [Medium effort] - file:line
3. [Larger change] - file:line
Risk: [Safe / Needs tests first]
Implement these changes?