Automates code quality improvements like method extraction, renaming, and pattern application.
Install
mkdir -p .claude/skills/refactor-tygwan && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/13031" && unzip -o skill.zip -d .claude/skills/refactor-tygwan && rm skill.zipInstalls to .claude/skills/refactor-tygwan
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Refactoring workflow. Improves code structure, reduces duplication, applies patterns. Use for code quality improvements.Key capabilities
- →Extract methods, classes, or modules from long functions
- →Rename variables, functions, or classes for clarity
- →Simplify complex conditional logic using guard clauses
- →Apply design patterns like Strategy to refactor switch statements
- →Analyze target code for smells like long methods or deep nesting
- →Plan changes by identifying issues and proposing solutions
How it works
The skill analyzes target code for smells, identifies refactoring opportunities, plans changes, and then executes them incrementally and safely, ensuring tests pass after each modification.
Inputs & outputs
When to use refactor
- →Extract long functions
- →Improve variable naming
- →Simplify nested conditionals
- →Apply design patterns
About this skill
Refactoring Skill
Usage
/refactor [target] [--type <refactoring>]
Parameters
target: File, function, or directory--type: extract | rename | simplify | pattern | all
Examples
/refactor src/utils.ts
/refactor src/api/ --type extract
/refactor handleSubmit --type simplify
/refactor src/services/ --type pattern
Refactoring Types
Extract (--type extract)
Extract methods, classes, or modules:
// Before: Long function
function processOrder(order) {
// validation (10 lines)
// calculation (15 lines)
// formatting (10 lines)
}
// After: Extracted functions
function processOrder(order) {
validateOrder(order);
const total = calculateTotal(order);
return formatResult(total);
}
Rename (--type rename)
Improve naming clarity:
// Before
const d = new Date();
const fn = (x) => x * 2;
class Mgr {}
// After
const currentDate = new Date();
const doubleValue = (value) => value * 2;
class OrderManager {}
Simplify (--type simplify)
Reduce complexity:
// Before: Nested conditions
if (user) {
if (user.isActive) {
if (user.hasPermission) {
return true;
}
}
}
return false;
// After: Guard clauses
if (!user) return false;
if (!user.isActive) return false;
if (!user.hasPermission) return false;
return true;
Pattern (--type pattern)
Apply design patterns:
// Before: Switch statement
function getDiscount(type) {
switch (type) {
case 'gold': return 0.2;
case 'silver': return 0.1;
default: return 0;
}
}
// After: Strategy pattern
const discounts = {
gold: { calculate: () => 0.2 },
silver: { calculate: () => 0.1 },
default: { calculate: () => 0 }
};
function getDiscount(type) {
return (discounts[type] || discounts.default).calculate();
}
Workflow
Step 1: Analyze Target
# Find code smells
wc -l {file} # Long files
Grep: "if.*if.*if|for.*for" # Deep nesting
Grep: "TODO|FIXME|HACK" # Technical debt markers
Step 2: Identify Opportunities
| Smell | Indicator | Refactoring |
|---|---|---|
| Long Method | >20 lines | Extract Method |
| Large Class | >300 lines | Extract Class |
| Long Params | >3 params | Parameter Object |
| Duplicate Code | Similar blocks | Extract Common |
| Deep Nesting | >3 levels | Guard Clauses |
| God Object | Does everything | Split Responsibilities |
Step 3: Plan Changes
## Refactoring Plan
### Target: src/orderService.ts
### Issues Found
1. `processOrder` is 85 lines (should be <20)
2. Duplicate validation in 3 methods
3. Deep nesting in `calculateDiscount`
### Proposed Changes
1. Extract `validateOrder()`, `calculateTotals()`, `applyDiscounts()`
2. Create `ValidationService` for shared validation
3. Use guard clauses in `calculateDiscount`
### Execution Order
1. Add tests for current behavior
2. Extract methods (preserving behavior)
3. Run tests after each change
4. Commit incrementally
Step 4: Execute Safely
- Verify tests exist
- Make one change
- Run tests
- Commit
- Repeat
Safety Checklist
Before refactoring:
- Tests exist for target code
- All tests pass
- Code is committed
After each change:
- Tests still pass
- Behavior unchanged
- Code compiles
After completion:
- All tests pass
- Code review complete
- Changes committed
Output Format
## Refactoring Report
### Target
`src/services/orderService.ts`
### Changes Made
| Line | Before | After |
|------|--------|-------|
| 45-85 | processOrder (40 lines) | processOrder (10 lines) + 3 helpers |
### New Functions
- `validateOrder(order)` - Validation logic
- `calculateTotals(items)` - Total calculation
- `applyDiscounts(total, customer)` - Discount logic
### Metrics
| Metric | Before | After |
|--------|--------|-------|
| Lines | 85 | 45 |
| Complexity | 12 | 4 |
| Functions | 1 | 4 |
### Tests
All 15 tests passing
Common Refactorings
| Technique | When to Use |
|---|---|
| Extract Method | Long function |
| Extract Variable | Complex expression |
| Inline Variable | Obvious temporary |
| Extract Class | Large class |
| Move Method | Feature envy |
| Replace Temp with Query | Calculated value |
| Replace Conditional with Polymorphism | Type-based switching |
| Introduce Parameter Object | Many parameters |
When not to use it
- →When tests do not exist for the target code
- →When all tests do not pass before refactoring
- →When code is not committed before refactoring
Limitations
- →Requires existing tests for the target code
- →Requires all tests to pass before refactoring begins
- →Requires code to be committed before refactoring
How it compares
This workflow provides a structured, test-driven approach to refactoring, ensuring code quality and behavior preservation, unlike ad-hoc code modifications.
Compared to similar skills
refactor side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| refactor (this skill) | 0 | 6mo | Review | Intermediate |
| typescript-expert | 10 | 6mo | Review | Advanced |
| agent-coder | 3 | 6mo | No flags | Intermediate |
| functional | 3 | 2mo | No flags | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
You might also like
typescript-expert
davila7
TypeScript and JavaScript expert with deep knowledge of type-level programming, performance optimization, monorepo management, migration strategies, and modern tooling. Use PROACTIVELY for any TypeScript/JavaScript issues including complex type gymnastics, build performance, debugging, and architectural decisions. If a specialized expert is a better fit, I will recommend switching and stop.
agent-coder
ruvnet
Agent skill for coder - invoke with $agent-coder
functional
citypaul
Functional programming patterns with immutable data. Use when writing logic or data transformations.
modern-javascript-patterns
sickn33
Master ES6+ features including async/await, destructuring, spread operators, arrow functions, promises, modules, iterators, generators, and functional programming patterns for writing clean, efficient JavaScript code. Use when refactoring legacy code, implementing modern patterns, or optimizing JavaScript applications.
typescript-lsp
Pouryaak
TypeScript language server providing type checking, code intelligence, and LSP diagnostics for .ts, .tsx, .js, .jsx, .mts, .cts, .mjs, .cjs files. Use when working with TypeScript or JavaScript code that needs type checking, autocomplete, error detection, refactoring support, or code navigation.
zustand
lobehub
Zustand state management guide. Use when working with store code (src/store/**), implementing actions, managing state, or creating slices. Triggers on Zustand store development, state management questions, or action implementation.