mcpField
A
ai-code-cleanup

ai-code-cleanup

github.com/89jobrien開発481 回インストール9 週間前に更新
リポジトリ

ai-code-cleanup について

ai-code-cleanupは、AIを使った開発後のコードを整理するスキルです。不要な防御ロジックやコメント、型の回避策、スタイルの不一致を対象にし、動作を保ちながらコンパイルやテストを確認します。

主な機能

  1. 01AI生成コードによくある痕跡の特定
  2. 02冗長なコメントと防御ロジックの削除
  3. 03不要な型回避策の置き換え
  4. 04プロジェクト規約に沿ったスタイル調整
  5. 05整理後のコンパイルとテストの確認

活用シーン

  1. 01AI支援によるコーディング後の整理
  2. 02レビュー用ブランチやプルリクエストの準備
  3. 03本番投入前のスタイル統一
  4. 04動作を変えずに冗長なコードを削減

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/89jobrien/steve --skill ai-code-cleanup

インストール後、Claude Code で/ai-code-cleanupを入力して読み込まれたか確認します。

同梱ファイルreferences/refactoring-plan.template.md

詳細

AI コードクリーンアップ

このスキルは、防御的なコードの肥大化、不要なコメント、型キャスト、スタイルの不統一など、コードの品質を低下させる AI 生成の成果物を特定して削除します。

このスキルを使用するタイミング

  • AI 支援によるコーディングセッションの後
  • コードレビューやブランチのマージの前
  • コードが「過剰設計」されていると感じるとき
  • 不要な防御的コードを削除するとき
  • AI による生成後にコードスタイルを標準化するとき
  • コードを本番環境に向けて準備するとき

このスキルでできること

  1. AI の成果物を特定する:AI 生成コードに典型的なパターンを検出します
  2. コードの肥大化を解消する:不要な防御的コードやコメントを取り除きます
  3. 型の問題を修正する:不要な型キャストや回避策を削除します
  4. スタイルを標準化する:プロジェクトの規約との一貫性を確保します
  5. 機能を維持する:品質を向上させながら、コードの動作を維持します
  6. 変更を検証する:コードが引き続きコンパイルされ、テストに合格することを確認します

使用方法

ブランチをクリーンアップする
Remove AI slop from this branch
Clean up the code in this pull request
特定の箇所をクリーンアップする
Remove unnecessary comments and defensive code from src/

削除する不要なコードのパターン

1. 不要なコメント

パターン:

  • 自明なコードを説明するコメント
  • ファイルのドキュメントスタイルと一貫性のないコメント
  • コードの内容を繰り返す冗長なコメント
  • 単純な処理に対する過剰な説明

例:

// ❌ AI-generated: Obvious comment
// Set the user's name
user.name = name;

// ✅ Clean: Self-documenting code
user.name = name;
2. 防御的なコードの肥大化

パターン:

  • コードベースで一般的ではない、余分な try/catch ブロック
  • 信頼できる経路での防御的な null/undefined チェック
  • 呼び出し元ですでに検証されている場合の冗長な入力検証
  • 発生する可能性のないエラー処理

例:

// ❌ AI-generated: Unnecessary defensive code
function processUser(user) {
  try {
    if (user && user.name && typeof user.name === 'string') {
      return user.name.toUpperCase();
    }
    return null;
  } catch (error) {
    console.error(error);
    return null;
  }
}

// ✅ Clean: Trust the input, handle real errors
function processUser(user) {
  return user.name.toUpperCase();
}
3. 型の回避策

パターン:

  • 型の問題を回避するための any へのキャスト
  • 不要な型アサーション(as X)
  • 正当な理由のない @ts-ignore または @ts-expect-error
  • 過度に複雑なジェネリック制約

例:

// ❌ AI-generated: Type workaround
const data = response.data as any;
const result = processData(data as ProcessedData);

// ✅ Clean: Proper typing
const data = response.data;
const result = processData(data);
4. スタイルの不統一

パターン:

  • ファイル内のほかの箇所と異なる命名規則
  • 周囲のコードと一致しない書式
  • ファイルのパターンと一貫性のないインポートの整理
  • ファイルのスタイルと一貫性のない変数宣言

例:

// ❌ AI-generated: Inconsistent style
const UserData = fetchUser();
var processedData = process(UserData);
let FinalResult = format(processedData);

// ✅ Clean: Consistent style
const userData = fetchUser();
const processedData = process(userData);
const finalResult = format(processedData);
5. AI 生成コードの兆候

パターン:

  • コードやコメントでの不要な絵文字の使用
  • 過度に冗長な変数名
  • 冗長な中間変数
  • 実際のユースケースがない「念のため」のコード

クリーンアップの手順

1. 差分を取得する
git diff $(git merge-base HEAD main)..HEAD --name-only
2. 各ファイルを分析する

変更されたファイルごとに:

  • 既存のスタイルを把握するため、ファイル全体を読みます
  • 不要なコードのカテゴリに該当するパターンを特定します
  • 不要なコードを取り除くため、必要な箇所だけを編集します
  • 機能を維持します
3. 検証
  • コードが引き続きコンパイルまたは実行されることを確認します
  • 変更によってコードが増えるのではなく、減るようにします
  • 動作を検証するためにテストを実行します
  • リグレッションがないか確認します

例

例 1:コメントのクリーンアップ

入力:src/auth.js から不要なコメントを削除する

出力:

## AI Code Cleanup: src/auth.js

### Removed
- 12 redundant comments explaining obvious code
- 3 comments inconsistent with file style
- 5 over-documented simple operations

### Changes
```javascript
// Before
// Function to validate user email
// Takes email string as input
// Returns boolean indicating validity
function validateEmail(email) {
  // Check if email is not empty
  if (!email) {
    return false; // Return false if empty
  }
  // Return regex test result
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}

// After
function validateEmail(email) {
  if (!email) return false;
  return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
}
結果
  • 行数を削減:12 → 4
  • 機能を維持
  • テストに合格

### Example 2: Defensive Bloat Removal

**Input**: Remove defensive bloat from `src/api/users.js`

**Output**:
```markdown
## AI Code Cleanup: src/api/users.js

### Removed
- 3 unnecessary try/catch blocks
- 8 redundant null checks on validated inputs
- 2 error handlers that can never trigger

### Changes
```javascript
// Before
async function getUser(userId) {
  try {
    if (!userId || typeof userId !== 'string') {
      throw new Error('Invalid userId');
    }
    const user = await db.users.findById(userId);
    if (user && user.id) {
      return user;
    }
    return null;
  } catch (error) {
    console.error(error);
    throw error;
  }
}

// After
async function getUser(userId) {
  const user = await db.users.findById(userId);
  return user || null;
}
結果
  • コードを削減:15 行 → 3 行
  • 機能を維持
  • コンテキストに適したエラー処理

## Reference Files

- **`references/REFACTORING_PLAN.template.md`** - Refactoring plan template with code smells, before/after metrics, and rollback strategy

## Best Practices

### Cleanup Guidelines

1. **Preserve Functionality**: Only remove code that doesn't affect behavior
2. **Maintain Style**: Follow existing project conventions
3. **Keep Real Errors**: Don't remove legitimate error handling
4. **Test After Changes**: Always verify code still works
5. **Incremental**: Make changes incrementally, test as you go

### What to Keep

- Legitimate error handling
- Necessary type assertions
- Helpful comments that add context
- Defensive code for untrusted inputs
- Style that matches the codebase

### What to Remove

- Obvious comments
- Unnecessary defensive code
- Type workarounds
- Style inconsistencies
- AI-generated artifacts

## Related Use Cases

- Post-AI coding cleanup
- Code review preparation
- Code quality improvement
- Style standardization
- Removing technical debt

コメント