Code Review Checklist with Claude Code
A practical code review checklist using Claude Code for improving code quality.
Claude Codeでコードレビューを効率化する
コードレビューは品質を維持するための重要なプロセスですが、レビュアーの負担が大きいのが課題です。Claude Codeを活用すれば、体系的なチェックリストに基づいた効率的なレビューが可能になります。
セキュリティチェック
> このPRのコードをセキュリティの観点でレビューして。
> XSS、SQLインジェクション、認証の脆弱性を確認して。
// セキュリティチェックリスト
const securityChecks = {
// 1. 入力バリデーション
inputValidation: {
check: 'ユーザー入力がサニタイズされているか',
bad: `element.innerHTML = userInput;`,
good: `element.textContent = userInput;`,
},
// 2. SQLインジェクション
sqlInjection: {
check: 'パラメータ化クエリが使われているか',
bad: `db.query(\`SELECT * FROM users WHERE id = \${userId}\`);`,
good: `db.query('SELECT * FROM users WHERE id = ?', [userId]);`,
},
// 3. 認証・認可
auth: {
check: 'APIエンドポイントに認証ミドルウェアがあるか',
bad: `app.get('/api/admin/users', handler);`,
good: `app.get('/api/admin/users', requireAuth, requireAdmin, handler);`,
},
// 4. 秘密情報の露出
secrets: {
check: '環境変数がクライアントに漏れていないか',
bad: `const apiKey = "sk-1234567890abcdef";`,
good: `const apiKey = process.env.API_KEY;`,
},
};
パフォーマンスチェック
// パフォーマンスチェックリスト
const performanceChecks = [
{
item: 'N+1クエリが発生していないか',
bad: `
const users = await db.user.findMany();
for (const user of users) {
const posts = await db.post.findMany({ where: { userId: user.id } });
}`,
good: `
const users = await db.user.findMany({
include: { posts: true },
});`,
},
{
item: '不必要な再レンダリングがないか',
bad: `
function Component() {
const handler = () => doSomething();
return <Child onClick={handler} />;
}`,
good: `
function Component() {
const handler = useCallback(() => doSomething(), []);
return <Child onClick={handler} />;
}`,
},
{
item: '大きなリストに仮想化が適用されているか',
check: '1000件以上のリスト → react-virtualを検討',
},
];
可読性・保守性チェック
> このコードの可読性を改善する提案をして。
> 命名規則、関数の長さ、コメントの適切さを確認して。
// 可読性チェックのポイント
// 1. 関数の長さ(30行以下が理想)
// ❌ 1つの関数で全処理
async function processOrder(order: Order) {
// 100行の処理...
}
// ✅ 責務ごとに分割
async function processOrder(order: Order) {
const validated = validateOrder(order);
const priced = calculateTotal(validated);
const payment = await processPayment(priced);
return createConfirmation(payment);
}
// 2. 早期リターンパターン
// ❌ ネストが深い
function getDiscount(user: User) {
if (user) {
if (user.isPremium) {
if (user.orders > 10) {
return 0.2;
}
return 0.1;
}
return 0.05;
}
return 0;
}
// ✅ 早期リターン
function getDiscount(user: User) {
if (!user) return 0;
if (!user.isPremium) return 0.05;
if (user.orders > 10) return 0.2;
return 0.1;
}
// 3. マジックナンバーの排除
// ❌
if (status === 3) { /* ... */ }
// ✅
const ORDER_STATUS = { PENDING: 1, PROCESSING: 2, COMPLETED: 3 } as const;
if (status === ORDER_STATUS.COMPLETED) { /* ... */ }
エラーハンドリングチェック
// エラーハンドリングの確認ポイント
// ❌ エラーの握りつぶし
try {
await saveData(data);
} catch (e) {
// 何もしない
}
// ✅ 適切なエラー処理
try {
await saveData(data);
} catch (error) {
logger.error('データ保存に失敗', { error, data: data.id });
throw new AppError('SAVE_FAILED', 'データの保存に失敗しました', { cause: error });
}
レビュー自動化スクリプト
// scripts/review-check.ts
import { execSync } from 'child_process';
interface ReviewIssue {
file: string;
line: number;
severity: 'error' | 'warning' | 'info';
message: string;
}
function checkForIssues(): ReviewIssue[] {
const issues: ReviewIssue[] = [];
const diff = execSync('git diff --cached --name-only').toString().split('\n');
for (const file of diff.filter(f => f.endsWith('.ts') || f.endsWith('.tsx'))) {
const content = execSync(`git show :${file}`).toString();
const lines = content.split('\n');
lines.forEach((line, index) => {
// console.logのチェック
if (line.includes('console.log') && !file.includes('scripts/')) {
issues.push({
file, line: index + 1, severity: 'warning',
message: 'console.logが残っています',
});
}
// TODO/FIXMEのチェック
if (/\/\/\s*(TODO|FIXME|HACK)/i.test(line)) {
issues.push({
file, line: index + 1, severity: 'info',
message: 'TODO/FIXMEコメントがあります',
});
}
// any型のチェック
if (/:\s*any\b/.test(line)) {
issues.push({
file, line: index + 1, severity: 'warning',
message: 'any型が使用されています',
});
}
});
}
return issues;
}
const issues = checkForIssues();
issues.forEach(i => console.log(`[${i.severity}] ${i.file}:${i.line} - ${i.message}`));
まとめ
コードレビューはテスト戦略と密接に関連します。Claude Codeを使えば、セキュリティ・パフォーマンス・可読性の観点から体系的なレビューを効率的に実施できます。AIペアプログラミングの手法と組み合わせれば、レビューの質と速度を両立できます。コードレビューのベストプラクティスはGoogle Engineering Practicesを参照してください。
#Claude Code
#code review
#quality assurance
#チェックリスト
#automation
Related Posts
Tips & Tricks
Tips & Tricks
10 Tips to Triple Your Productivity with Claude Code
Learn about 10 tips to triple your productivity using Claude Code. Practical tips and code examples included.
Tips & Tricks
Tips & Tricks
Canvas/WebGL Optimization with Claude Code
Learn about canvas/webgl optimization using Claude Code. Practical tips and code examples included.
Tips & Tricks
Tips & Tricks
Markdown Implementation with Claude Code
Learn about markdown implementation using Claude Code. Practical tips and code examples included.