refactor-with-ai
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.
What this skill does
# Refactor with AI
Safely refactor code with AI assistance while preserving behavior.
## When to Use
- Cleaning up messy or complex code
- Extracting reusable functions/components
- Modernizing legacy patterns
- Reducing cyclomatic complexity
- Applying design patterns
- Preparing code for new features
## The Safe Refactoring Process
```
┌─────────────────────────────────────────┐
│ 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 │
└─────────────────────────────────────────┘
```
## Step 1: Characterize with Tests
Before ANY refactoring, ensure behavior is captured.
```typescript
// 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' });
});
});
```
## Step 2: Identify Code Smells
### Common Smells to Look For
| 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 |
### AI Prompt for Identification
```markdown
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.
```
## Step 3: Refactor Incrementally
### Pattern: Extract Function
**Before:**
```typescript
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:**
```markdown
Extract the validation logic from this function into a separate
`validateOrder` function. Keep the same behavior.
```[paste function]```
```
**After:**
```typescript
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...
}
```
### Pattern: Replace Conditionals with Guard Clauses
**Before:**
```typescript
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:**
```markdown
Refactor this function using guard clauses and early returns
to reduce nesting. Keep exact same behavior.
```[paste function]```
```
**After:**
```typescript
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';
}
```
### Pattern: Introduce Parameter Object
**Before:**
```typescript
function createUser(
firstName: string,
lastName: string,
email: string,
phone: string,
address: string,
city: string,
country: string,
role: string
) {
// ...
}
```
**AI Prompt:**
```markdown
Refactor this function to use a parameter object instead of
individual parameters. Create appropriate TypeScript interface.
```[paste function]```
```
**After:**
```typescript
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) {
// ...
}
```
## Step 4: Verify Behavior
After each refactoring step:
```bash
# Run existing tests
npm test
# Run type checking
npx tsc --noEmit
# Run linting
npm run lint
# If available, run mutation testing
npx stryker run
```
### Verification Checklist
- [ ] All existing tests pass
- [ ] No new TypeScript errors
- [ ] No new linting errors
- [ ] Manual smoke test of affected feature
- [ ] Performance not degraded (if applicable)
## Refactoring Prompts Library
### Simplify Complex Logic
```markdown
Simplify this code while preserving exact behavior.
Focus on readability and reducing complexity.
```
### Modernize Patterns
```markdown
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 Component (React)
```markdown
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
```
### Apply Design Pattern
```markdown
Refactor this code to use the [Strategy/Factory/Observer] pattern.
Current code: ```[paste]```
Goal: Make it easier to [add new types/extend behavior/etc]
```
## Common Mistakes to Avoid
1. **Refactoring without tests** - You can't verify behavior preservation
2. **Too many changes at once** - Small steps, verify each
3. **Changing behavior "while we're at it"** - Separate commits
4. **Not running tests after each step** - Bugs compound
5. **Over-engineering** - Extract only what's needed now
## When NOT to Refactor
- Code works and rarely changes
- No tests and can't add them
- Under time pressure for unrelated feature
- Major rewrite is actually needed
Related in Design
contribute
IncludedLocal-only OSS contribution command center. Auto-refreshes the user's in-flight PR and issue state on invoke so conversations start with full context — no need to brief Claude on what's in flight. Helps the user find issues to contribute to on GitHub, builds per-repo dossiers of what each upstream expects (CLA, DCO, branch convention, AI policy, draft-first, review bots, issue templates), runs deterministic gates before any external action so AI-assisted contributions don't reach maintainers as slop. State is markdown-only: candidate files at ~/.contribute-system/candidates/, repo dossiers at ~/.contribute-system/research/, append-only event log at ~/.contribute-system/log.jsonl. No database, no cloud calls. Use when the user asks about their PRs / issues / contributions, wants to find new work to take on, claim an issue, build/refresh a repo's dossier, or draft a Design Issue or PR. Trigger with "/contribute", "what's my PR status", "find a contribution", "claim issue X", "draft a Design Issue for Y", "refresh dossier for Z".
architectural-analysis
IncludedUser-triggered deep architectural analysis of a codebase or scoped subtree across eight modes — information architecture, data flow, integration points, UI surfaces, interaction patterns, data model, control flow, and failure modes. This skill should be used when the user asks to "diagram this codebase," "map the architecture," "show the data flow," "give me an ERD," "trace control flow," "find the integration points," "verify the layout pattern," "audit the UX architecture," or any similar request whose primary deliverable is mermaid diagrams plus cited reports under docs/architecture/. Dispatches haiku/sonnet sub-agents in parallel for per-mode exploration, then verifies every citation mechanically before any node lands in a diagram. Not for one-off prose explanations of code (use code-explanation) or for high-level system design from scratch (use system-design).
mcp
IncludedModel Context Protocol (MCP) server development and tool management. Languages: Python, TypeScript. Capabilities: build MCP servers, integrate external APIs, discover/execute MCP tools, manage multi-server configs, design agent-centric tools. Actions: create, build, integrate, discover, execute, configure MCP servers/tools. Keywords: MCP, Model Context Protocol, MCP server, MCP tool, stdio transport, SSE transport, tool discovery, resource provider, prompt template, external API integration, Gemini CLI MCP, Claude MCP, agent tools, tool execution, server config. Use when: building MCP servers, integrating external APIs as MCP tools, discovering available MCP tools, executing MCP capabilities, configuring multi-server setups, designing tools for AI agents.
react-native-skia
IncludedDesign, build, debug, and optimise high-polish animated graphics in React Native or Expo using @shopify/react-native-skia, Reanimated, and Gesture Handler. Use when the user wants canvas-driven UI, shaders, paths, rich text, image filters, sprite fields, Skottie, video frames, snapshots, web CanvasKit setup, or performance tuning for custom motion-heavy elements such as loaders, hero art, cards, charts, progress indicators, particle systems, or gesture-driven surfaces. Also use when the user asks for fluid, glow, glass, blob, parallax, 60fps/120fps, or GPU-friendly animated effects in React Native, even if they do not explicitly say "Skia". Do not use for ordinary form/layout work with standard views.
plaid
IncludedProduct Led AI Development — guides founders from idea to launched product. Six capabilities: Idea (discover a product idea), Validate (pressure-test the idea against fatal flaws, problem reality, competition, and 2-week MVP feasibility), Plan (vision intake + document generation), Design (translate image references into a design.md spec), Launch (go-to-market strategy), and Build (roadmap execution). Use when someone says "PLAID", "plaid idea", "help me find an idea", "product idea", "idea from my business", "idea from my expertise", "plaid validate", "validate my idea", "pressure-test", "is this idea good", "find fatal flaws", "validate the problem", "plan a product", "define my vision", "generate a PRD", "product strategy", "plaid design", "design from image", "translate image to design", "create design.md", "extract design tokens", "plaid launch", "go-to-market", "launch plan", "GTM strategy", "launch playbook", "plaid build", "build the app", "start building", or "execute the roadmap".
nextjs-framer-motion-animations
IncludedAdds production-safe Motion for React or Framer Motion animations to Next.js apps, including reveal, hover and tap micro-interactions, whileInView, stagger, AnimatePresence, layout and layoutId transitions, reorder, scroll-linked UI, and lightweight route-content transitions. Use when the user asks to add, refactor, or debug Motion or Framer Motion in App Router or Pages Router codebases, especially around server/client boundaries, reduced motion, LazyMotion, bundle size, hydration, or route transitions. Avoid for GSAP-style timelines, WebGL or 3D scenes, heavy scroll storytelling, or CSS-only effects unless Motion is explicitly requested.