with one click
refactor
Safe code refactoring with proper testing and incremental changes
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Menu
Safe code refactoring with proper testing and incremental changes
Install with Codex or Claude Copy this prompt, paste it into Codex, Claude, or another assistant, and let it review the skill page and install it for you.
Based on SOC occupation classification
Systematic approach to debugging and fixing issues
Create well-formatted commits following conventional commit standards
Create well-documented pull requests with proper descriptions
Guide for integrating external APIs securely and correctly
Reviews pull requests for code quality, security, and conventions
Test-Driven Development workflow - write tests first, then implement
| name | Refactor |
| description | Safe code refactoring with proper testing and incremental changes |
| triggers | ["/refactor","refactor this","clean up code","restructure"] |
| allowed-tools | ["Read","Write","Edit","Glob","Grep","Bash(npm run test*)","Bash(pnpm test*)","Bash(yarn test*)","Bash(npm run lint*)","Bash(git diff*)","Bash(git status*)"] |
Safely restructure code without changing behavior.
Refactoring changes HOW code works, not WHAT it does.
All tests should pass before AND after refactoring.
Before refactoring, verify tests exist:
# Run existing tests
npm run test
# Check coverage for the code you're refactoring
npm run test -- --coverage src/module-to-refactor/
If coverage is low, add tests first before refactoring.
Each step should be:
Bad: One massive commit that changes everything Good: Series of small commits, each improving the code
# Quick feedback loop
npm run test -- --watch src/module/
git commit -m "refactor(auth): extract token validation to separate function"
git commit -m "refactor(auth): rename validateToken to verifyJWT"
git commit -m "refactor(auth): move JWT logic to dedicated service"
When: Code block does one specific thing
// Before
function processOrder(order) {
// 20 lines calculating total
// 15 lines validating inventory
// 10 lines sending notifications
}
// After
function processOrder(order) {
const total = calculateTotal(order);
validateInventory(order.items);
sendOrderNotifications(order);
}
When: Related functions should be grouped
// Before: utils.ts with 50 functions
// After:
// date-utils.ts - date formatting functions
// string-utils.ts - string manipulation
// validation-utils.ts - validators
When: Names don't describe purpose
// Before
const d = new Date();
function proc(x) { ... }
// After
const createdAt = new Date();
function processPayment(transaction) { ... }
When: Switch/if chains based on type
// Before
function calculateArea(shape) {
if (shape.type === 'circle') return Math.PI * shape.radius ** 2;
if (shape.type === 'rectangle') return shape.width * shape.height;
}
// After
class Circle {
calculateArea() { return Math.PI * this.radius ** 2; }
}
class Rectangle {
calculateArea() { return this.width * this.height; }
}
When: Same code appears in multiple places
// Before: Same validation in 3 files
// After: Shared validation utility
import { validateEmail } from '@/utils/validation';
When: Complex boolean logic
// Before
if (user && user.subscription && user.subscription.active && !user.banned) {
// ...
}
// After
function canAccessPremiumContent(user) {
if (!user) return false;
if (user.banned) return false;
return user.subscription?.active ?? false;
}
if (canAccessPremiumContent(user)) {
// ...
}
When in doubt, commit what you have and reassess.
| Smell | Refactoring |
|---|---|
| Long function | Extract functions |
| Long parameter list | Introduce parameter object |
| Duplicate code | Extract shared function |
| Large class | Split into focused classes |
| Feature envy | Move method to appropriate class |
| Primitive obsession | Create domain types |
| Deep nesting | Extract functions, early returns |