skills/developer/code-intelligence/refactor-with-ai/SKILL.md
Use this skill when refactoring code with AI assistance. Activate when the user wants to improve code structure, extract functions, reduce complexity, modernize legacy code, apply design patterns, clean up technical debt, or restructure code while preserving behavior.
npx skillsauth add latestaiagents/agent-skills refactor-with-aiInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
Safely refactor code with AI assistance while preserving behavior.
┌─────────────────────────────────────────┐
│ 1. CHARACTERIZE - Understand current │
│ behavior with tests │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ 2. IDENTIFY - Find specific smells │
│ and improvement opportunities │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ 3. REFACTOR - Small, incremental │
│ changes with AI assistance │
└─────────────────────────────────────────┘
↓
┌─────────────────────────────────────────┐
│ 4. VERIFY - Tests still pass, │
│ behavior unchanged │
└─────────────────────────────────────────┘
Before ANY refactoring, ensure behavior is captured.
// If no tests exist, create characterization tests
describe('OrderProcessor (characterization)', () => {
it('processes valid order', () => {
const result = processor.process(validOrder);
// Capture current behavior, even if not ideal
expect(result).toMatchSnapshot();
});
it('handles edge cases', () => {
expect(processor.process(null)).toBeNull();
expect(processor.process(emptyOrder)).toEqual({ status: 'empty' });
});
});
| Smell | Sign | Refactoring | |-------|------|-------------| | Long Method | >20 lines | Extract Method | | Long Parameter List | >3 params | Introduce Parameter Object | | Duplicate Code | Copy-paste | Extract and reuse | | Nested Conditionals | >3 levels deep | Guard clauses, early return | | Feature Envy | Uses other class's data | Move method | | Data Clumps | Same params together | Create class/type | | Primitive Obsession | Strings for everything | Value objects | | Switch Statements | Type checking | Polymorphism |
Analyze this code for refactoring opportunities. List:
1. Code smells present
2. Complexity hotspots
3. Potential extractions
4. Modernization opportunities
```[paste code]```
Focus on actionable improvements, not stylistic preferences.
Before:
function processOrder(order: Order) {
// 50 lines of validation
if (!order.items) return { error: 'No items' };
if (order.items.length === 0) return { error: 'Empty order' };
if (!order.customer) return { error: 'No customer' };
// ... more validation
// 30 lines of calculation
let total = 0;
for (const item of order.items) {
total += item.price * item.quantity;
if (item.discount) {
total -= item.discount;
}
}
// ... more calculation
// 20 lines of persistence
// ...
}
AI Prompt:
Extract the validation logic from this function into a separate
`validateOrder` function. Keep the same behavior.
```[paste function]```
After:
function validateOrder(order: Order): ValidationResult {
if (!order.items) return { valid: false, error: 'No items' };
if (order.items.length === 0) return { valid: false, error: 'Empty order' };
if (!order.customer) return { valid: false, error: 'No customer' };
return { valid: true };
}
function calculateTotal(items: OrderItem[]): number {
return items.reduce((total, item) => {
const itemTotal = item.price * item.quantity;
return total + itemTotal - (item.discount || 0);
}, 0);
}
function processOrder(order: Order) {
const validation = validateOrder(order);
if (!validation.valid) return { error: validation.error };
const total = calculateTotal(order.items);
// persistence...
}
Before:
function getPaymentStatus(payment: Payment) {
if (payment) {
if (payment.status) {
if (payment.status === 'completed') {
if (payment.amount > 0) {
return 'success';
} else {
return 'zero-amount';
}
} else {
return payment.status;
}
} else {
return 'unknown';
}
} else {
return 'no-payment';
}
}
AI Prompt:
Refactor this function using guard clauses and early returns
to reduce nesting. Keep exact same behavior.
```[paste function]```
After:
function getPaymentStatus(payment: Payment) {
if (!payment) return 'no-payment';
if (!payment.status) return 'unknown';
if (payment.status !== 'completed') return payment.status;
if (payment.amount <= 0) return 'zero-amount';
return 'success';
}
Before:
function createUser(
firstName: string,
lastName: string,
email: string,
phone: string,
address: string,
city: string,
country: string,
role: string
) {
// ...
}
AI Prompt:
Refactor this function to use a parameter object instead of
individual parameters. Create appropriate TypeScript interface.
```[paste function]```
After:
interface CreateUserParams {
name: {
first: string;
last: string;
};
contact: {
email: string;
phone: string;
};
address: {
street: string;
city: string;
country: string;
};
role: string;
}
function createUser(params: CreateUserParams) {
// ...
}
After each refactoring step:
# Run existing tests
npm test
# Run type checking
npx tsc --noEmit
# Run linting
npm run lint
# If available, run mutation testing
npx stryker run
Simplify this code while preserving exact behavior.
Focus on readability and reducing complexity.
Modernize this code to use current JavaScript/TypeScript patterns:
- async/await instead of callbacks/promises
- Optional chaining and nullish coalescing
- Array methods instead of loops where appropriate
Preserve all existing behavior.
Extract the [specific part] into a separate reusable component.
- Keep the same props interface visible
- Maintain all existing behavior
- Follow the project's component patterns
Refactor this code to use the [Strategy/Factory/Observer] pattern.
Current code: ```[paste]```
Goal: Make it easier to [add new types/extend behavior/etc]
development
Test skills for correct activation, content quality, and regression — both automated checks (frontmatter validity, lint) and manual verification (query-suite activation testing). Covers CI integration and how to catch skill regressions before users do. Use this skill when adding skills to a repo, setting up CI for a skill library, or debugging "the skill exists but doesn't work". Activate when: test skills, validate skills, skill CI, skill linting, skill activation test, skill regression.
documentation
Write the YAML frontmatter for a SKILL.md file so it activates reliably — name, description, and activation keywords that the model matches against. Covers length, tone, and the most common frontmatter mistakes. Use this skill when authoring a new skill, fixing a skill that isn't auto-activating, or reviewing skills for publication. Activate when: SKILL.md frontmatter, skill description, skill activation, skill YAML, write a skill, author a skill.
development
Design skills that fire at the right moment — neither over-eager (noise) nor under-eager (silent). Covers activation specificity, trigger phrases, disambiguation between overlapping skills, and debugging activation. Use this skill when multiple skills could fire on the same query, a skill never fires, or a skill fires too often. Activate when: skill won't activate, skill over-activates, overlapping skills, skill triggers, skill selection, skill disambiguation.
development
Structure SKILL.md content so the model reads just enough — concise summary up front, progressively deeper detail, examples on demand. Covers section ordering, length budgets, when to split into multiple skills. Use this skill when writing or refactoring a skill body, one skill has grown too long, or a skill is wordy but not useful. Activate when: SKILL.md structure, skill content, skill too long, split skill, progressive disclosure, skill body.