一键导入
refactoring
Refactors code safely with test verification. Use when asked to refactor, restructure, clean up, or improve code organization.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
菜单
Refactors code safely with test verification. Use when asked to refactor, restructure, clean up, or improve code organization.
用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
基于 SOC 职业分类
Complete the current milestone and prepare next work by following the canonical complete command doc.
Execute the current plan (from STATE.md -> PLAN.md) by following the canonical execute command doc. Make code changes + run necessary checks.
Initialize project with CLAUDE.md, STATE.md, and ROADMAP.md by following the canonical init command doc.
Create executable plan for the current phase (3-5 tasks). Reads the canonical plan command doc and produces/updates the current PLAN.md referenced from STATE.md. No implementation.
Review code changes and capture learnings for CLAUDE.md by following the canonical review command doc.
Check project progress and suggest the next action by following the canonical status command doc.
| name | refactoring |
| description | Refactors code safely with test verification. Use when asked to refactor, restructure, clean up, or improve code organization. |
Restructure code safely while preserving behavior.
Before refactoring, ensure tests exist:
# Run existing tests
npm test
pytest
go test ./...
If no tests cover the code:
Map the code:
# Find usages
grep -r "functionName" --include="*.ts"
# Find dependencies
grep -r "import.*from.*module" --include="*.ts"
Identify the target state:
Choose your approach:
| Situation | Approach |
|---|---|
| Large function | Extract methods |
| Duplicated code | Extract shared function |
| Complex conditionals | Replace with polymorphism |
| Long parameter list | Introduce parameter object |
| Feature envy | Move method to data's class |
| Data clump | Extract class |
The safe refactoring cycle:
1. Make ONE small change
2. Run tests
3. Commit if green
4. Repeat
Never skip steps:
# Run full test suite
npm test
# Check for regressions
npm run test:e2e
# Manual smoke test if needed
Most IDEs can do these automatically:
| Refactoring | Shortcut (VS Code) |
|---|---|
| Rename | F2 |
| Extract function | Ctrl+Shift+R |
| Extract variable | Ctrl+Shift+R |
| Inline variable | Ctrl+Shift+R |
| Move to file | Drag in explorer |
// Before
function processOrder(order) {
// validate
if (!order.items.length) throw new Error('Empty order');
if (!order.customer) throw new Error('No customer');
// calculate total
let total = 0;
for (const item of order.items) {
total += item.price * item.quantity;
}
// apply discount
if (order.coupon) {
total *= (1 - order.coupon.discount);
}
return { ...order, total };
}
// After
function processOrder(order) {
validateOrder(order);
const total = calculateTotal(order);
return { ...order, total };
}
function validateOrder(order) {
if (!order.items.length) throw new Error('Empty order');
if (!order.customer) throw new Error('No customer');
}
function calculateTotal(order) {
const subtotal = order.items.reduce(
(sum, item) => sum + item.price * item.quantity,
0
);
return order.coupon
? subtotal * (1 - order.coupon.discount)
: subtotal;
}
// Before
function getSpeed(vehicle) {
switch (vehicle.type) {
case 'car': return vehicle.horsepower * 0.5;
case 'bike': return vehicle.gears * 5;
case 'boat': return vehicle.engineSize * 2;
}
}
// After
class Car {
getSpeed() { return this.horsepower * 0.5; }
}
class Bike {
getSpeed() { return this.gears * 5; }
}
class Boat {
getSpeed() { return this.engineSize * 2; }
}
// Before
function createUser(name, email, age, country, role) {
// ...
}
// After
function createUser({ name, email, age, country, role }) {
// ...
}
// Or with type
interface CreateUserParams {
name: string;
email: string;
age: number;
country: string;
role: string;
}
function createUser(params: CreateUserParams) {
// ...
}
| Smell | Refactoring |
|---|---|
| Long method | Extract method |
| Large class | Extract class |
| Duplicate code | Extract function |
| Long parameter list | Parameter object |
| Switch statements | Polymorphism |
| Feature envy | Move method |
| Data clump | Extract class |
| Primitive obsession | Value object |
| Comments explaining code | Extract well-named method |
Refactor and change behavior simultaneously
Make big-bang changes
Refactor without tests
Over-abstract too early
Refactor "just in case"