refactoring-specialist by charon-fan/agent-playbook
npx skills add https://github.com/charon-fan/agent-playbook --skill refactoring-specialist提供重构代码的专业指导,以改善代码结构、可读性和可维护性,同时保持功能不变。
当您进行以下操作时,此技能会触发:
症状: 函数超过 20-30 行
重构方法: 提取方法
// Before:
function processOrder(order) {
// 50 lines of code
}
// After:
function processOrder(order) {
validateOrder(order);
calculateTotals(order);
saveOrder(order);
sendConfirmation(order);
}
症状: 相似代码出现在多个地方
重构方法: 提取方法 / 模板方法
// 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;
}
}
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
症状: 一个类承担了太多职责
重构方法: 提取类
// 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 个
重构方法: 引入参数对象
// 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;
}
症状: 方法使用了其他类的更多数据
重构方法: 移动方法
// Before:
class Order {
calculatePrice(customer) {
const discount = customer.getDiscountLevel();
// ...
}
}
// After:
class Customer {
calculatePriceForOrder(order) {
const discount = this.discountLevel;
// ...
}
}
症状: 相同的数据总是一起出现
重构方法: 提取值对象
// 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) { ... }
症状: 使用基本类型而非小对象
重构方法: 用对象替换基本类型
// Before:
function createUser(name, email, phone) { ... }
// After:
class Email {
constructor(value) {
if (!this.isValid(value)) throw new Error('Invalid email');
this.value = value;
}
// ...
}
症状: 基于类型的大型 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; }
}
症状: 变量仅在特定场景下使用
重构方法: 提取类
// Before:
class User {
calculateRefund() {
this.tempRefundAmount = 0;
// complex calculation
return this.tempRefundAmount;
}
}
// After:
class RefundCalculator {
calculate(user) {
// ...
}
}
症状: 代码需要大量注释才能理解
重构方法: 提取方法并使用清晰的名称
// 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);
}
references/smells.md - 完整的代码异味目录references/techniques.md - 重构技术references/checklist.md - 重构检查清单每周安装次数
59
代码仓库
GitHub 星标数
11
首次出现
2026年1月22日
安全审计
安装于
codex51
gemini-cli50
opencode49
cursor47
github-copilot45
kimi-cli43
Expert guidance on refactoring code to improve structure, readability, and maintainability while preserving functionality.
Activates when you:
Symptom: Function > 20-30 lines
Refactoring: Extract Method
// Before:
function processOrder(order) {
// 50 lines of code
}
// After:
function processOrder(order) {
validateOrder(order);
calculateTotals(order);
saveOrder(order);
sendConfirmation(order);
}
Symptom: Similar code in multiple places
Refactoring: Extract Method / Template Method
// 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;
}
}
Symptom: Class doing too many things
Refactoring: Extract Class
// Before:
class User {
// Authentication
// Profile management
// Notifications
// Reporting
}
// After:
class User { /* Core user data */ }
class UserAuth { /* Authentication */ }
class UserProfile { /* Profile management */ }
class UserNotifier { /* Notifications */ }
Symptom: Function with 4+ parameters
Refactoring: Introduce Parameter Object
// 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;
}
Symptom: Method uses more data from other classes
Refactoring: Move Method
// Before:
class Order {
calculatePrice(customer) {
const discount = customer.getDiscountLevel();
// ...
}
}
// After:
class Customer {
calculatePriceForOrder(order) {
const discount = this.discountLevel;
// ...
}
}
Symptom : Same data appearing together
Refactoring : Extract Value Object
// 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) { ... }
Symptom : Using primitives instead of small objects
Refactoring : Replace Primitive with Object
// Before:
function createUser(name, email, phone) { ... }
// After:
class Email {
constructor(value) {
if (!this.isValid(value)) throw new Error('Invalid email');
this.value = value;
}
// ...
}
Symptom : Large switch on type
Refactoring : Replace Conditional with Polymorphism
// 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; }
}
Symptom : Variables only used in certain scenarios
Refactoring : Extract Class
// Before:
class User {
calculateRefund() {
this.tempRefundAmount = 0;
// complex calculation
return this.tempRefundAmount;
}
}
// After:
class RefundCalculator {
calculate(user) {
// ...
}
}
Symptom : Code needs extensive comments
Refactoring : Extract Method with clear name
// 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);
}
references/smells.md - Complete code smell catalogreferences/techniques.md - Refactoring techniquesreferences/checklist.md - Refactoring checklistWeekly Installs
59
Repository
GitHub Stars
11
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
codex51
gemini-cli50
opencode49
cursor47
github-copilot45
kimi-cli43
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
111,800 周安装