rea-analysis-optimization
MOmorluto/rea486k8,943 new stars in the last 7 days
Martin Fowler 著『Refactoring: Improving the Design of Existing Code』(第 2 版) に基づくコードリファクタリングへの体系的アプローチ。本スキルは、テストに支えられた安全で段階的な変更を重視する。
"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
開始前に次の点を明確化する。
ユーザーへの提示内容:
"Refactoring without tests is like driving without a seatbelt." — Martin Fowler
テストは安全なリファクタリングを可能にする鍵となる要素である。テストがなければ、バグを混入させるリスクがある。
既存テストの確認
# テストファイルを探す
find . -name "*test*" -o -name "*spec*" | head -20
既存テストの実行
# JavaScript/TypeScript
npm test
# Python
pytest -v
# Java
mvn test
カバレッジの確認(可能な場合)
# JavaScript
npm run test:coverage
# Python
pytest --cov=.
テストが存在し、かつパスしている場合:
テストが不足、または不完全な場合: 次の選択肢を提示する。
テストが失敗している場合:
リファクタリング対象の各関数について、以下をテストでカバーする。
「red-green-refactor」サイクルを用いる。
コードに潜むより深い問題の症状である。バグそのものではないが、コード改善の余地を示す指標となる。
完全なカタログは references/code-smells.md を参照。
| スメル | 兆候 | 影響 |
|---|---|---|
| Long Method(長すぎる関数) | メソッドが 30〜50 行を超える | 理解、テスト、保守が困難 |
| Duplicated Code(重複したコード) | 同じロジックが複数箇所に存在 | バグ修正を複数箇所で行う必要がある |
| Large Class(巨大なクラス) | 責務が多すぎるクラス | 単一責任原則に違反 |
| Feature Envy(機能の横恋慕) | 他クラスのデータを多く使用するメソッド | カプセル化が不十分 |
| Primitive Obsession(プリミティブ型への執着) | オブジェクト化すべき所をプリミティブで多用 | ドメイン概念が欠落 |
| Long Parameter List(長すぎるパラメータリスト) | パラメータが 4 個以上 | 正しく呼び出すのが困難 |
| Data Clumps(データの群れ) | 同じデータ項目が常に一緒に現れる | 抽象化が欠落 |
| Switch Statements(スイッチ文) | 複雑な switch / if-else の連鎖 | 拡張が困難 |
| Speculative Generality(投機的一般化) | 「念のため」のコード | 不要な複雑さ |
| Dead Code(デッドコード) | 使われていないコード | 混乱と保守負担 |
自動分析(スクリプトが利用可能な場合)
python scripts/detect-smells.py <file>
手動レビュー
優先順位付け 次のようなスメルに注力する。
ユーザーへの提示内容:
各スメルに対して、カタログから適切なリファクタリング技法を選ぶ。
完全な一覧は references/refactoring-catalog.md を参照。
| コードスメル | 推奨リファクタリング |
|---|---|
| Long Method(長すぎる関数) | メソッドの抽出、Replace Temp with Query |
| Duplicated Code(重複したコード) | メソッドの抽出、Pull Up Method、Form Template Method |
| Large Class(巨大なクラス) | クラスの抽出、Extract Subclass |
| Feature Envy(機能の横恋慕) | Move Method、Move Field |
| Primitive Obsession(プリミティブ型への執着) | Replace Primitive with Object、Replace Type Code with Class |
| Long Parameter List(長すぎるパラメータリスト) | パラメータオブジェクトの導入、Preserve Whole Object |
| Data Clumps(データの群れ) | クラスの抽出、パラメータオブジェクトの導入 |
| Switch Statements(スイッチ文) | ポリモーフィズムによる条件記述の置き換え |
| Speculative Generality(投機的一般化) | Collapse Hierarchy、Inline Class、デッドコードの削除 |
| Dead Code(デッドコード) | デッドコードの削除 |
templates/refactoring-plan.md のテンプレートを使用する。
各リファクタリングごとに次の項目を埋める。
重要: リファクタリングはフェーズに分けて段階的に導入する。
フェーズ A: クイックウィン(低リスク、高価値)
フェーズ B: 構造改善(中リスク)
フェーズ C: アーキテクチャ変更(高リスク)
実装前に:
"Change → Test → Green? → Commit → Next step"
各リファクタリングステップで:
事前チェック
小さな変更を 1 つだけ行う
検証
テストがパス(green)した場合
テストが失敗(red)した場合
各コミットは次の性質を満たすこと。
コミットメッセージ例:
refactor: Extract calculateTotal() from processOrder()
refactor: Rename 'x' to 'customerCount' for clarity
refactor: Remove unused validateOldFormat() method
サブフェーズごとに、ユーザーへ次を報告する。
リファクタリング前後で複雑度解析を実施する。
python scripts/analyze-complexity.py <file>
改善点を提示する。
最終結果を提示する。
ユーザーと議論する。
次の場合、必ず一旦止まりユーザーに相談する。
Before:
function processOrder(order) {
// 150 行のコードに以下が混在:
// - 重複したバリデーションロジック
// - インラインの計算処理
// - 複数の責務
}
リファクタリング手順:
After:
function processOrder(order) {
validateOrder(order);
const total = calculateOrderTotal(order);
notifyCustomer(order, total);
return { order, total };
}
scripts/analyze-complexity.py - コード複雑度メトリクスの分析scripts/detect-smells.py - スメルの自動検出MOmorluto/rea486k8,943 new stars in the last 7 days
Diagnose hard bugs and performance regressions by building a feedback loop with tests or scripts.
MAmattpocock/skills337k4,978 new stars in the last 7 days
MAmattpocock/skills337k4,978 new stars in the last 7 days
MAmattpocock/skills337k4,978 new stars in the last 7 days
MAmattpocock/skills337k4,978 new stars in the last 7 days
MAmattpocock/skills337k4,978 new stars in the last 7 days