refactor
できること
このスキルは、エージェントが動作を変えずに、既存コードの構造を小さくテストされた手順で改善するよう導きます。古いコードを整理したり、技術的負債を安全に減らしたりしたい開発者向けです。
使う場面
- ✓動作を変えずに、雑然とした古いコードを整理したいとき。
- ✓重複した関数や長すぎる関数などのコードの臭いを取り除きたいとき。
- ✓変更を加える前に、リファクタリングの計画を承認してから進めたいとき。
スキルのリンク
https://github.com/luongnv89/claude-howto/blob/main/03-skills/refactor/SKILL.mdコピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。
使い方
いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。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
コメント
- …
このリポジトリの他のスキル
lesson-quiz
46kClaude Codeのチュートリアルの1つのレッスンについて、10問のクイズを出し、回答を採点して、復習すべき項目を指摘します。
self-assessment
46kClaude Codeの10の機能領域についてクイズを出し、トピック別の点数と、次に学ぶ内容の学習パスを示します。
blog-draft
46kアイデアと情報源からブログ記事を下書きする作業を、段階を追って案内します。各段階で版を追跡し、あなたの承認を得ながら進めます。
claude-md
46kエージェントがCLAUDE.mdファイルを作成・改善できるよう支援します。CLAUDE.mdは、AIエージェントにプロジェクトを紹介するガイドです。
似ているスキル
diffs
394k編集内容の実際の共有可能な差分を、手書きの要約ではなく、ビューアーのリンクや画像ファイルとして作成するようエージェントを支援します。
coding-agent
394kCodex、Claude Code、OpenCodeに大きめのコーディング作業をバックグラウンドのワーカーとして渡し、その進捗を確認するようエージェントを支援します。
gh-issues
394kGitHubのイシューを探し、取り組むものを選び、修正を含むプルリクエストを開くようエージェントを支援します。レビューコメントへの対応も含みます。