重构顾问 - 识别代码坏味道并提供重构方案
npx skills add https://github.com/laolaoshiren/claude-code-skills-zh --skill refactor-advisor
当用户要求重构代码、优化代码结构、改善代码质量、消除技术债务时激活此技能。
grep/rg 扫描目标目录,识别潜在坏味道按严重程度分类发现的问题:
| 等级 | 含义 | 说明 |
|------|------|------|
| 🔴 严重 | 必须立即修复 | 影响可维护性和稳定性 |
| 🟡 中等 | 建议尽快修复 | 降低代码可读性 |
| 🟢 轻微 | 可择机优化 | 不影响功能但有改进空间 |
按以下维度综合排序:
| 坏味道 | 特征 | 重构手法 | 等级 |
|--------|------|---------|------|
| 长方法 | 函数超过 50 行 | 提取方法 (Extract Method) | 🔴 |
| 大类 | 类超过 300 行 | 提取类 (Extract Class) | 🔴 |
| 重复代码 | 相似代码块出现 3+ 次 | 提取公共方法 | 🔴 |
| 过长参数 | 函数参数超过 4 个 | 引入参数对象 | 🟡 |
| 嵌套过深 | if/for 嵌套 3+ 层 | 卫语句 (Guard Clause) | 🟡 |
| 魔法数字 | 硬编码的数字 | 提取为常量 | 🟢 |
| 过长链式调用 | a.b.c.d.e | 提取中间变量 | 🟢 |
| 全局可变状态 | 全局变量被多处修改 | 封装为模块/引入依赖注入 | 🔴 |
| 过度耦合 | 模块间相互依赖 | 依赖反转、引入接口 | 🟡 |
# 代码重构报告
## 概要
- 扫描文件数:XXX
- 发现问题数:XX(🔴 X / 🟡 X / 🟢 X)
- 预估工作量:X 天
## 问题列表
### 问题 1:[问题标题]
- **📍 位置**:`src/services/user.ts:45-120`
- **🔍 问题**:该函数长达 75 行,包含 3 层嵌套,职责不单一
- **🛠️ 重构方案**:
- 提取 `validateUserData()` 方法
- 提取 `saveToDatabase()` 方法
- 使用卫语句替代嵌套 if
- **⚠️ 风险评估**:
- 该函数被 12 处调用,需全面回归测试
- 建议先添加单元测试再重构
- **Before**:
function processUser(data) { /* 75行... */ }
- **After**:
function processUser(data) {
const validated = validateUserData(data);
return saveToDatabase(validated);
}
## 重构路线图
| 阶段 | 内容 | 优先级 | 预估 |
|------|------|--------|------|
| P1 | 修复 🔴 严重问题 | 高 | 2天 |
| P2 | 处理 🟡 中等问题 | 中 | 3天 |
| P3 | 优化 🟢 轻微问题 | 低 | 1天 |
Use when receiving code review feedback, before implementing suggestions, especially if feedback seems unclear or technically questionable - requires technical rigor and verification, not performative agreement or blind implementation
Use when completing tasks, implementing major features, or before merging to verify work meets requirements
Execute git commit with conventional commit message analysis, intelligent staging, and message generation. Use when user asks to commit changes, create a git commit, or mentions "/commit". Supports: (1) Auto-detecting type and scope from changes, (2) Generating conventional commit messages from diff, (3) Interactive commit with optional type/scope/description overrides, (4) Intelligent file staging for logical grouping
Comprehensive GitHub code review with AI-powered swarm coordination
Behavioral guidelines to reduce common LLM coding mistakes. Use when writing, reviewing, or refactoring code to avoid overcomplication, make surgical changes, surface assumptions, and define verifiable success criteria.
Use this skill to review code. It supports both local changes (staged or working tree) and remote Pull Requests (by ID or URL). It focuses on correctness, maintainability, and adherence to project standards.
Refactor bloated AGENTS.md, CLAUDE.md, or similar agent instruction files to follow progressive disclosure principles. Splits monolithic files into organized, linked documentation.
Create high-quality git commits: review/stage intended changes, split into logical commits, and write clear commit messages (including Conventional Commits). Use when the user asks to commit, craft a commit message, stage changes, or split work into multiple commits.
Take laolaoshiren/refactor-advisor from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.