refactor: コードリファクタリング
外部の動作を変更しないコード再構築のためのrefactorコミットタイプの使い方を学びます。refactor、fix、featの違いを理解します。
Type Examples
詳細な説明
refactor コミットタイプ
refactorタイプは、バグの修正でも機能の追加でもないコード変更を示します。外部の動作を変えずに、可読性、保守性、パフォーマンス特性を改善するために既存コードを再構築します。Semantic Versioningでは、refactorコミットはバージョンバンプをトリガーしません。
メッセージの例
refactor: extract validation logic into separate module
refactor(auth): replace callback-based flow with async/await
refactorを使うタイミング
以下の場合にrefactorを使用します:
- 変数、関数、ファイルを明確にするためにリネームする
- コードを再利用可能な関数やモジュールに抽出する
- 複雑な条件分岐ロジックを簡素化する
- ファイルやディレクトリ構造を再編成する
- 実装パターンを別のものに置き換える(例:コールバックからPromise)
- デッドコードや未使用の依存関係を削除する
重要なルール:動作を変更してはいけない
決定的な違いは、refactorは観測可能な動作を変更してはいけないことです。再構築が:
- バグを修正する →
fixを使用 - 新しいケイパビリティを追加する →
featを使用 - 測定可能なパフォーマンスを改善する →
perfを使用
良いリファクタリングの説明
| 悪い例 | 良い例 |
|---|---|
refactor: refactor code |
refactor: extract email validation into util |
refactor: clean up |
refactor: simplify nested conditionals in parser |
refactor: misc changes |
refactor: replace class component with hook |
コンテキスト付きのボディ
refactor: extract validation logic into separate module
Move email, phone, and address validation functions from
UserController into a dedicated ValidationService. This
reduces the controller's responsibility and makes the
validators reusable across other controllers.
No behavioral changes - all existing tests pass.
ユースケース
複雑な関数をいくつかのネストされた条件分岐を名前の付いたヘルパー関数に抽出することで簡素化しました。すべてのテストが通り出力は同じですが、コードがはるかに読みやすく保守しやすくなりました。機能的な変更を示唆せずに再構築を説明するコミットメッセージが必要です。