关于 refactoring-specialist
refactoring-specialist 是面向开发者的代码重构指导技能,可识别常见代码异味并推荐渐进式改进方法,强调保持行为不变并通过测试验证。
主要功能
- 01识别代码异味
- 02推荐重构技术
- 03渐进式且保持行为不变
- 04重构前后测试检查
- 05重构前后检查清单
适用场景
- 01拆分过长方法或大型类
- 02消除重复代码
- 03通过测试规划安全重构
- 04检查重构准备情况与结果
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ zhaono1/ agent-playbook --skill refactoring-specialist安装后在 Claude Code 中输入/refactoring-specialist确认已加载。
包含的文件
除 SKILL.md 外,安装时一起下载的参考资料和脚本。
README.mdreferences/checklist.mdsmells.mdtechniques.md详细介绍
重构专家
提供关于重构代码的专业指导,以改善代码结构、可读性和可维护性,同时保留原有功能。
此技能的触发条件
在以下情况下触发:
- 要求重构代码
- 请求清理或改进代码
- 提到“技术债”或“代码异味”
- 希望提升代码质量
重构原则
- 保留行为:重构不得改变外部行为
- 小步进行:进行小幅、渐进式修改
- 测试覆盖:确保修改前后测试均通过
- 频繁提交:每次成功重构后都提交
需要处理的代码异味
1. 过长方法
症状: 函数超过 20~30 行
重构方式: 提取方法
// Before:
function processOrder(order) {
// 50 lines of code
}
// After:
function processOrder(order) {
validateOrder(order);
calculateTotals(order);
saveOrder(order);
sendConfirmation(order);
}
2. 重复代码
症状: 多处存在相似代码
重构方式: 提取方法 / 模板方法
// Before:
class UserService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
class AdminService {
async validateEmail(email) {
if (!email || !email.includes('@')) return false;
const domain = email.split('@')[1];
return domain.length > 0;
}
}
// After:
class EmailValidator {
async validate(email) {
if (!email || !email.includes('@')) return false;
return email.split('@')[1].length > 0;
}
}
3. 过大的类
症状: 类承担了过多职责
重构方式: 提取类
// Before:
class User {
// Authentication
// Profile management
// Notifications
// Reporting
}
// After:
class User { /* Core user data */ }
class UserAuth { /* Authentication */ }
class UserProfile { /* Profile management */ }
class UserNotifier { /* Notifications */ }
4. 参数列表过长
症状: 函数有 4 个或更多参数
重构方式: 引入参数对象
// Before:
function createUser(name, email, age, address, phone, role) { ... }
// After:
function createUser(user: UserData) { ... }
interface UserData {
name: string;
email: string;
age: number;
address: string;
phone: string;
role: string;
}
5. 特权依恋
症状: 方法使用其他类的数据较多
重构方式: 移动方法
// Before:
class Report {
formatSummary(formatter) {
const options = formatter.getFormattingOptions();
// ...
}
}
// After:
class Formatter {
formatReport(report) {
const discount = this.discountLevel;
// ...
}
}
6. 数据泥团
症状:相同的数据总是一起出现
重构方式:提取值对象
// Before:
function drawShape(x, y, width, height) { ... }
function moveShape(x, y, width, height, dx, dy) { ... }
// After:
class Rectangle {
constructor(x, y, width, height) { ... }
}
function drawShape(rect: Rectangle) { ... }
7. 基本类型偏执
症状:使用基本类型,而非小型对象
重构方式:以对象替换基本类型
// Before:
function createUser(name, email, phone) { ... }
// After:
class Email {
constructor(value) {
if (!this.isValid(value)) throw new Error('Invalid email');
this.value = value;
}
// ...
}
8. Switch 语句
症状:对类型进行大量 switch 判断
重构方式:以多态替换条件判断
// Before:
function calculatePay(employee) {
switch (employee.type) {
case 'engineer': return employee.salary * 1.2;
case 'manager': return employee.salary * 1.5;
case 'sales': return employee.salary * 1.1;
}
}
// After:
interface Employee {
calculatePay(): number;
}
class Engineer implements Employee {
calculatePay() { return this.salary * 1.2; }
}
9. 临时字段
症状:变量只在特定场景中使用
重构方式:提取类
// Before:
class User {
calculateRefund() {
this.tempRefundAmount = 0;
// complex calculation
return this.tempRefundAmount;
}
}
// After:
class RefundCalculator {
calculate(user) {
// ...
}
}
10. 注释
症状:代码需要大量注释
重构方式:提取方法并使用清晰的名称
// Before:
// Calculate the total price including discounts
// and tax based on user location
function calc(u, i) {
let t = 0;
// discount logic
if (u.vip) t *= 0.9;
// tax logic
if (u.state === 'CA') t *= 1.08;
return t;
}
// After:
function calculateTotalPrice(user: User, items: Item[]): number {
let total = items.sum(i => i.price);
if (user.isVIP) {
total = applyVIPDiscount(total);
}
return applyTax(total, user.state);
}
重构步骤
- 识别异味 - 是什么让这段代码难以处理?
- 确定重构方式 - 适用哪种技术?
- 确保测试通过 - 开始前先确认测试通过
- 应用重构 - 进行修改
- 运行测试 - 确认行为未发生变化
- 提交 - 进行小型、原子化提交
安全重构实践
- 使用 IDE 的重构工具(重命名、提取、移动)
- 经常运行测试(每次修改后)
- 保持提交小而专注
- 编写描述清晰的提交消息
- 对复杂重构考虑进行代码审查
重构前
- 测试均已通过
- 我了解代码的作用
- 我已识别出具体的代码异味
- 我知道要采用哪种重构方式
- 我有回滚计划
重构后
- 测试仍然通过
- 代码更易读
- 代码更易维护
- 没有引入新的代码异味
- 如有需要,已更新文档
参考资料
references/smells.md- 完整的代码异味目录references/techniques.md- 重构技术references/checklist.md- 重构检查清单
评论
支持 Markdown