03-skills/refactor/SKILL.md
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.
npx skillsauth add luongnv89/claude-howto code-refactorInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
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
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
Before starting, clarify:
Present findings to user:
"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.
Check for existing tests
# Look for test files
find . -name "*test*" -o -name "*spec*" | head -20
Run existing tests
# JavaScript/TypeScript
npm test
# Python
pytest -v
# Java
mvn test
Check coverage (if available)
# JavaScript
npm run test:coverage
# Python
pytest --cov=.
If tests exist and pass:
If tests are missing or incomplete: Present options:
If tests are failing:
For each function being refactored, ensure tests cover:
Use the "red-green-refactor" cycle:
Symptoms of deeper problems in code. They're not bugs, but indicators that the code could be improved.
See references/code-smells.md for the complete catalog.
| 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 |
Automated Analysis (if scripts available)
python scripts/detect-smells.py <file>
Manual Review
Prioritization Focus on smells that:
Present to user:
For each smell, select an appropriate refactoring from the catalog.
See references/refactoring-catalog.md for the complete list.
| Code Smell | Recommended Refactoring(s) | |------------|---------------------------| | Long Method | Extract Method, Replace Temp with Query | | Duplicated Code | Extract Method, Pull Up Method, Form Template Method | | Large Class | Extract Class, Extract Subclass | | Feature Envy | Move Method, Move Field | | Primitive Obsession | Replace Primitive with Object, Replace Type Code with Class | | Long Parameter List | Introduce Parameter Object, Preserve Whole Object | | Data Clumps | Extract Class, Introduce Parameter Object | | Switch Statements | Replace Conditional with Polymorphism | | Speculative Generality | Collapse Hierarchy, Inline Class, Remove Dead Code | | Dead Code | Remove Dead Code |
Use the template at templates/refactoring-plan.md.
For each refactoring:
CRITICAL: Introduce refactoring gradually in phases.
Phase A: Quick Wins (Low risk, high value)
Phase B: Structural Improvements (Medium risk)
Phase C: Architectural Changes (Higher risk)
Before implementation:
"Change → Test → Green? → Commit → Next step"
For each refactoring step:
Pre-check
Make ONE small change
Verify
If tests pass (green)
If tests fail (red)
Each commit should be:
Example commit messages:
refactor: Extract calculateTotal() from processOrder()
refactor: Rename 'x' to 'customerCount' for clarity
refactor: Remove unused validateOldFormat() method
After each sub-phase, report to user:
Run complexity analysis before and after:
python scripts/analyze-complexity.py <file>
Present improvements:
Present final results:
Discuss with user:
Always pause and consult user when:
Before:
function processOrder(order) {
// 150 lines of code with:
// - Duplicated validation logic
// - Inline calculations
// - Mixed responsibilities
}
Refactoring Steps:
After:
function processOrder(order) {
validateOrder(order);
const total = calculateOrderTotal(order);
notifyCustomer(order, total);
return { order, total };
}
scripts/analyze-complexity.py - Analyze code complexity metricsscripts/detect-smells.py - Automated smell detectiondevelopment
提供全面的代码审查能力,覆盖安全、性能和代码质量分析。适用于用户请求代码审查、代码质量评估、Pull Request 审查,或提到安全分析和性能优化时。
development
Review code toàn diện với phân tích bảo mật, hiệu suất, và chất lượng. Sử dụng khi người dùng yêu cầu review code, phân tích chất lượng code, đánh giá pull requests, hoặc đề cập đến review code, phân tích bảo mật, hoặc tối ưu hóa hiệu suất.
development
Комплексне код-рев'ю з аналізом безпеки, продуктивності та якості. Використовуйте, коли користувачі просять переглянути код, проаналізувати якість коду, оцінити pull request, або згадують код-рев'ю, аналіз безпеки чи оптимізацію продуктивності.
development
<!-- i18n-source: 03-skills/code-review-specialist/SKILL.md --> <!-- i18n-source-sha: e978c49 --> <!-- i18n-date: 2026-04-27 --> --- name: code-review-specialist description: セキュリティ、パフォーマンス、品質分析を含む包括的なコードレビュー。コードレビュー、コード品質分析、プルリクエスト評価の依頼があった場合、またはコードレビュー、セキュリティ分析、パフォーマンス最適化について言及がある場合に使用する。 --- # コードレビュー・スキル このスキルは、以下に焦点を当てた包括的なコードレビュー機能を提供する。 1. **セキュリティ分析** - 認証・認可の問題 - データ漏洩リスク - インジェクション脆弱性 - 暗号の弱点 - 機密データのロギング 2. **パフォーマンス・レビュー** - アルゴリズム効率(Big O 分析) - メモリ最適化 -