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 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 detectionLast Updated: August 4, 2026 Claude Code Version: 2.1.220 Sources:
development
Comprehensive code review with security, performance, and quality analysis. Use when users ask to review code, analyze code quality, evaluate pull requests, or mention code review, security analysis, or performance optimization.
development
<!-- i18n-source: 03-skills/refactor/SKILL.md --> <!-- i18n-source-sha: 245272f --> <!-- i18n-date: 2026-04-27 --> --- name: refactor description: Martin Fowler の方法論に基づく体系的なコードリファクタリング。ユーザーがコードのリファクタリング、コード構造の改善、技術的負債の削減、レガシーコードのクリーンアップ、コードスメルの解消、コード保守性の向上を求めた際に使用する。本スキルは、リサーチ・計画・安全な段階的実装からなる段階的アプローチを案内する。 --- # コードリファクタリングスキル Martin Fowler 著『Refactoring: Improving the Design of Existing Code』(第 2 版) に基づくコードリファクタリングへの体系的アプローチ。本スキルは、テストに支えられた安全で段階的な変更を重視する。 > "Refactoring is the process of cha
development
<!-- i18n-source: 03-skills/doc-generator/SKILL.md --> <!-- i18n-source-sha: a6380d8 --> <!-- i18n-date: 2026-04-27 --> --- name: doc-generator description: ソースコードから包括的かつ正確な API ドキュメントを生成する。API ドキュメントの作成・更新、OpenAPI 仕様の生成時、または API ドキュメント、エンドポイント、ドキュメントについて言及がある場合に使用する。 --- # API ドキュメント生成スキル ## 生成するもの - OpenAPI/Swagger 仕様 - API エンドポイントのドキュメント - SDK 利用例 - 統合ガイド - エラーコード・リファレンス - 認証ガイド ## ドキュメント構造 ### 各エンドポイントごと ````markdown ## GET /api/v1/users/:id ### Description このエンドポイントの動作を簡潔に説明 ### Pa
development
<!-- i18n-source: 03-skills/claude-md/SKILL.md --> <!-- i18n-source-sha: f78c094 --> <!-- i18n-date: 2026-04-27 --> --- name: claude-md description: Create or update CLAUDE.md files following best practices for optimal AI agent onboarding --- ## ユーザー入力 ```text $ARGUMENTS ``` ユーザー入力が空でない場合、進める前に **必ず** 内容を考慮すること。ユーザーは以下を指定する場合がある: - `create` - 新しい CLAUDE.md をゼロから作成 - `update` - 既存 CLAUDE.md を改善 - `audit` - 現在の CLAUDE.md の品質を分析しレポート - 作成・更新する具体的なパス(例: ディレクトリ固有命令向けの `src/api/CLAUDE.md`) ## 基本原則