这个技能使代理能够识别重构机会并提供系统化的重构方案。
Detects code smells and provides systematic refactoring plans with step-by-step guidance. Triggers when you encounter complex functions, duplicated code, or design issues during code review or development.
/plugin marketplace add Protagonistss/claude-plugins/plugin install dev-tools@claude-plugins-protagonisthsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
这个技能使代理能够识别重构机会并提供系统化的重构方案。
代码重构技能使代理能够:
每次重构改动要小,频繁提交:
重构前后功能不变:
不追求一步到位:
时机:函数过长、代码重复、注释解释代码
重构前:
function printOwing(invoice) {
printBanner();
// 计算未付金额
let outstanding = 0;
for (const order of invoice.orders) {
outstanding += order.amount;
}
// 打印详情
console.log(`客户:${invoice.customer}`);
console.log(`未付金额:${outstanding}`);
}
重构后:
function printOwing(invoice) {
printBanner();
const outstanding = calculateOutstanding(invoice);
printDetails(invoice, outstanding);
}
function calculateOutstanding(invoice) {
let result = 0;
for (const order of invoice.orders) {
result += order.amount;
}
return result;
}
function printDetails(invoice, outstanding) {
console.log(`客户:${invoice.customer}`);
console.log(`未付金额:${outstanding}`);
}
时机:函数体比函数名更清晰
重构前:
function getRating(driver) {
return moreThanFiveLateDeliveries(driver) ? 2 : 1;
}
function moreThanFiveLateDeliveries(driver) {
return driver.numberOfLateDeliveries > 5;
}
重构后:
function getRating(driver) {
return driver.numberOfLateDeliveries > 5 ? 2 : 1;
}
时机:表达式难以理解
重构前:
function price(order) {
return order.quantity * order.itemPrice -
Math.max(0, order.quantity - 500) * order.itemPrice * 0.05 +
Math.min(order.quantity * order.itemPrice * 0.1, 100);
}
重构后:
function price(order) {
const basePrice = order.quantity * order.itemPrice;
const quantityDiscount = Math.max(0, order.quantity - 500) * order.itemPrice * 0.05;
const shipping = Math.min(basePrice * 0.1, 100);
return basePrice - quantityDiscount + shipping;
}
时机:一组参数总是一起出现
重构前:
function amountInvoiced(startDate, endDate) { }
function amountReceived(startDate, endDate) { }
function amountOverdue(startDate, endDate) { }
重构后:
class DateRange {
constructor(startDate, endDate) {
this.startDate = startDate;
this.endDate = endDate;
}
}
function amountInvoiced(dateRange) { }
function amountReceived(dateRange) { }
function amountOverdue(dateRange) { }
重构前:
function getSpeed(bird) {
switch (bird.type) {
case 'European':
return getEuropeanSpeed(bird);
case 'African':
return getAfricanSpeed(bird);
case 'Norwegian':
return getNorwegianSpeed(bird);
default:
throw new Error('Unknown bird');
}
}
重构后:
class Bird {
getSpeed() {
throw new Error('Must be implemented by subclass');
}
}
class EuropeanBird extends Bird {
getSpeed() {
// 欧洲燕的速度计算
}
}
class AfricanBird extends Bird {
getSpeed() {
// 非洲燕的速度计算
}
}
class NorwegianBird extends Bird {
getSpeed() {
// 挪威燕的速度计算
}
}
function getSpeed(bird) {
return bird.getSpeed();
}
重构前:
let youngest = people[0] ? people[0].age : Infinity;
let totalSalary = 0;
for (const person of people) {
if (person.age < youngest) youngest = person.age;
totalSalary += person.salary;
}
return { youngest, totalSalary };
重构后:
function youngestAge() {
let youngest = people[0] ? people[0].age : Infinity;
for (const person of people) {
if (person.age < youngest) youngest = person.age;
}
return youngest;
}
function totalSalary() {
let total = 0;
for (const person of people) {
total += person.salary;
}
return total;
}
return {
youngest: youngestAge(),
totalSalary: totalSalary()
};
重构前:
function example() {
doSomething();
// 这段代码已经不用了
// if (oldFeature) {
// doOldThing();
// }
doAnotherThing();
}
重构后:
function example() {
doSomething();
doAnotherThing();
}
1. 代码审查
- 人工审查
- 静态分析工具
- 代码度量
2. 问题分类
- 命名问题
- 结构问题
- 设计问题
3. 优先级排序
- 影响程度
- 修复难度
- 风险评估
1. 选择重构技巧
- 匹配问题类型
- 考虑代码规模
- 评估风险
2. 拆分步骤
- 小步重构
- 定义检查点
- 准备回滚
3. 准备测试
- 现有测试
- 补充测试
- 集成测试
1. 运行测试
- 确保测试通过
- 记录基准
2. 应用重构
- 一次一个改动
- 频繁运行测试
- 及时提交
3. 验证结果
- 测试通过
- 功能正常
- 性能无退化
1. 代码清理
- 移除注释
- 统一格式
- 更新文档
2. 性能检查
- 基准测试
- 性能对比
- 优化瓶颈
3. 团队评审
- Code Review
- 知识分享
- 经验总结
适用:大函数拆分
步骤1:识别代码块
步骤2:提取第一个函数
步骤3:测试
步骤4:提交
步骤5:重复 2-4
适用:接口变更
步骤1:创建新接口
步骤2:新旧接口并存
步骤3:逐步迁移调用方
步骤4:所有调用方迁移完成
步骤5:删除旧接口
适用:大规模重构
步骤1:创建特性分支
步骤2:在分支上重构
步骤3:保持分支同步主干
步骤4:通过测试
步骤5:合并回主干
现代 IDE 提供安全的重构:
重构时持续运行测试:
# 监视文件变化,自动运行测试
npm run test:watch
每完成一个小重构就提交:
git add .
git commit -m "refactor: 提取 calculateTotal 函数"
两人一起重构:
❌ 一次重构太多
❌ 没有测试就重构
❌ 重构时添加新功能
❌ 过度重构
这个技能会被以下代理使用:
注意:CodeReviewer 功能已迁移到独立的 code-review 插件,提供更专业的代码审查和重构建议。