refactor
它能做什么
它指导你的智能体在不改变代码功能的前提下,分小步、有测试地改进现有代码的结构。适合想安全地整理遗留代码或减少技术债的开发者。
适用场景
- ✓当你想在不改变行为的前提下,整理杂乱或遗留代码的时候。
- ✓当你想消除代码坏味道,例如重复或过长的函数的时候。
- ✓当你希望在改动前先批准重构计划的时候。
技能链接
https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请帮我安全地重构这个遗留的计费模块,先给我一份我可以审批的计划。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会研究代码、检查测试覆盖率、找出代码坏味道,并写出计划。之后它每次只做一小步改动,并在每个阶段请你批准。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Code Refactoring Skill
A systematic approach to refactoring code based on Martin Fowler's Refactoring: Improving the Design of Existing Code (2nd Edition). This skill emphasizes safe, incremental changes backed by tests.
"Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure." — Martin Fowler
Core Principles
- Behavior Preservation: External behavior must remain unchanged
- Small Steps: Make tiny, testable changes
- Test-Driven: Tests are the safety net
- Continuous: Refactoring is ongoing, not a one-time event
- Collaborative: User approval required at each phase
Workflow Overview
Phase 1: Research & Analysis
↓
Phase 2: Test Coverage Assessment
↓
Phase 3: Code Smell Identification
↓
Phase 4: Refactoring Plan Creation
↓
Phase 5: Incremental Implementation
↓
Phase 6: Review & Iteration
Phase 1: Research & Analysis
Objectives
- Understand the codebase structure and purpose
- Identify the scope of refactoring
- Gather context about business requirements
Questions to Ask User
Before starting, clarify:
- Scope: Which files/modules/functions need refactoring?
- Goals: What problems are you trying to solve? (readability, performance, maintainability)
- Constraints: Are there any areas that should NOT be changed?
- Timeline pressure: Is this blocking other work?
- Test status: Do tests exist? Are they passing?
Actions
- Read and understand the target code
- Identify dependencies and integrations
- Document current architecture
- Note any existing technical debt markers (TODOs, FIXMEs)
Output
Present findings to user:
- Code structure summary
- Identified problem areas
- Initial recommendations
- Request approval to proceed
Phase 2: Test Coverage Assessment
Why Tests Matter
"Refactoring without tests is like driving without a seatbelt." — Martin Fowler
Tests are the key enabler of safe refactoring. Without them, you risk introducing bugs.
Assessment Steps
Check for existing tests
# Look for test files find . -name "*test*" -o -name "*spec*" | head -20Run existing tests
# JavaScript/TypeScript npm test # Python pytest -v # Java mvn testCheck coverage (if available)
# JavaScript npm run test:coverage # Python pytest --cov=.
Decision Point: Ask User
If tests exist and pass:
- Proceed to Phase 3
If tests are missing or incomplete: Present options:
- Write tests first (recommended)
- Add tests incrementally during refactoring
- Proceed without tests (risky - requires user acknowledgment)
If tests are failing:
- STOP. Fix failing tests before refactoring
- Ask user: Should we fix tests first?
Test Writing Guidelines (if needed)
For each function being refactored, ensure tests cover:
- Happy path (normal operation)
- Edge cases (empty inputs, null, boundaries)
- Error scenarios (invalid inputs, exceptions)
Use the "red-green-refactor" cycle:
- Write failing test (red)
- Make it pass (green)
- Refactor
Phase 3: Code Smell Identification
What Are Code Smells?
Symptoms of deeper problems in code. They're not bugs, but indicators that the code could be improved.
Common Code Smells to Check
See references/code-smells.md for the complete catalog.
Quick Reference
| Smell | Signs | Impact |
|---|---|---|
| Long Method | Methods > 30-50 lines | Hard to understand, test, maintain |
| Duplicated Code | Same logic in multiple places | Bug fixes needed in multiple places |
| Large Class | Class with too many responsibilities | Violates Single Responsibility |
| Feature Envy | Method uses another class's data more | Poor encapsulation |
| Primitive Obsession | Overuse of primitives instead of objects | Missing domain concepts |
| Long Parameter List | Methods with 4+ parameters | Hard to call correctly |
| Data Clumps | Same data items appearing together | Missing abstraction |
| Switch Statements | Complex switch/if-else chains | Hard to extend |
| Speculative Generality | Code "just in case" | Unnecessary complexity |
| Dead Code | Unused code | Confusion, maintenance burden |
Analysis Steps
- **Automated
常见问题
它会改变行为吗?
技能要求行为必须保持不变,因此重构只改变内部结构。
需要测试吗?
技能会先评估测试覆盖率,并用测试作为每次改动的安全网。
它会不经询问就改代码吗?
不会。技能要求每个阶段都必须获得你的批准。
详情
作者的原始描述
Systematic code refactoring based on Martin Fowler's methodology. Use when users ask to refactor code, improve code structure, reduce technical debt, clean up legacy code, eliminate code smells, or improve code maintainability. This skill guides through a phased approach with research, planning, and safe incremental implementation.
repo: luongnv89/claude-howto
热度
- GitHub 星标
- 46k
- 排名
- #6324
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …