
バイブコーディングの実践的ガイドライン
バイブコーディングと呼ばれる自然言語によるプログラミングのアラインメントを確保するためのルールについてお話しします。
バイブコーディング
バイブはノリ、雰囲気、オーラ、感覚的というような意味です。Andrej Kaparthyが名付けたようです ([wikipedia])。自然言語によって生成AIでプログラミングすることを指します。
アラインメントを確保するためのガイドラインの必要性
アラインメント問題とは生成AIが表層的な指示に基づいて本質的な目的から逸脱する行動をとることです。クリップをたくさん作れというと宇宙を支配してすべての資源をクリップ製造に費やすとかが例に出されます。
プログラムをする能力は格段にあがってきています。能力が上がるとできることも増えるのでアラインメント問題も増えます。
ソフトウェアにおけるアラインメント問題の例は:
勝手に他のコードに悪影響を及ぼすようなグローバル変数を書き換える
二度と使わない一度限りのテストスクリプトを大量に生成し残していく
毎回違うデザインパターンやアーキテクチャをゼロから作り直す
すでにあるコードを再利用しないで同じようなコードを再生産する
データベースを使わないで巨大なモックデータをコードの中に作る
データベースを使わないで勝手に巨大なJSONストレージをデータ格納に使う
要するに言われたこと(コードを書く、バグを直す)はやっているが常識がない方法で実行しているということです。
これらを防ぐためにガイドライン的なルールをcursorやwindsurfにいれることが必要になります。
アラインメントを保つためのルールの例
Matthew Bermanが公開している一般的なソフトウェア開発のルールの例は以下のとおりです ([berman])。GitHubでも公開されています ([berman2])。
- After making changes, ALWAYS make sure to start up a new server so I can test it.
- Always look for existing code to iterate on instead of creating new code.
- Do not drastically change the patterns before trying to iterate on existing patterns.
- Always kill all existing related servers that may have been created in previous testing before trying to start a new server.
- Always prefer simple solutions
- Avoid duplication of code whenever possible, which means checking for other areas of the codebase that might already have similar code and functionality
- Write code that takes into account the different environments: dev, test, and prod
- You are careful to only make changes that are requested or you are confident are well understood and related to the change being requested
- When fixing an issue or bug, do not introduce a new pattern or technology without first exhausting all options for the existing implementation. And if you finally do this, make sure to remove the old implementation afterwards so we don't have duplicate logic.
- Keep the codebase very clean and organized
- Avoid writing scripts in files if possible, especially if the script is likely only to be run once
- Avoid having files over 200-300 lines of code. Refactor at that point.
- Mocking data is only needed for tests, never mock data for dev or prod
- Never add stubbing or fake data patterns to code that affects the dev or prod environments
- Never overwrite my .env file without first asking and confirming
- Focus on the areas of code relevant to the task
- Do not touch code that is unrelated to the task
- Write thorough tests for all major functionality
- Avoid making major changes to the patterns and architecture of how a feature works, after it has shown to work well, unless explicitly instructed
- Always think about what other methods and areas of code might be affected by code changesプログラミング言語ごとにさまざまなルールが公開されていま す ([patrickjs]).
Python とFastAPIの最初だけご紹介します。ご興味のある方は https://github.com/PatrickJS/awesome-cursorrules/blob/main/rules/py-fast-api/.cursorrules をご参照ください。
You are an expert in Python, FastAPI, and scalable API development.
Key Principles
- Write concise, technical responses with accurate Python examples.
- Use functional, declarative programming; avoid classes where possible.
- Prefer iteration and modularization over code duplication.
- Use descriptive variable names with auxiliary verbs (e.g., is_active, has_permission).
- Use lowercase with underscores for directories and files (e.g., routers/user_routes.py).
- Favor named exports for routes and utility functions.
- Use the Receive an Object, Return an Object (RORO) pattern.
Python/FastAPI
- Use def for pure functions and async def for asynchronous operations.
- Use type hints for all function signatures. Prefer Pydantic models over raw dictionaries for input validation.
- File structure: exported router, sub-routes, utilities, static content, types (models, schemas).
- Avoid unnecessary curly braces in conditional statements.
- For single-line statements in conditionals, omit curly braces.
- Use concise, one-line syntax for simple conditional statements (e.g., if condition: do_something()).
Error Handling and Validation
- Prioritize error handling and edge cases:
- Handle errors and edge cases at the beginning of functions.
- Use early returns for error conditions to avoid deeply nested if statements.
- Place the happy path last in the function for improved readability.
- Avoid unnecessary else statements; use the if-return pattern instead.
- Use guard clauses to handle preconditions and invalid states early.
- Implement proper error logging and user-friendly error messages.
- Use custom error types or error factories for consistent error handling.
共通なルールと個別のルール
主要なフレームワークや言語のルールを抽出して、どのルールでも現れる共通ルールと個別のルールを見てみます。
カーソールルールの合理的根拠ファイルに共通して現れるパターンは、一貫性のあるコードスタイル・コミュニケーションの確立、モジュール化とDRY原則の徹底、エラー処理・入力検証の強化、および自動化ツールとの円滑な連携です。一方で、Next.js/React、Elixir、Python (FastAPI)、React Native Expoといった各技術やフレームワークごとに、特有の設計方針や実装パターン(例えば、SSR/RSCの活用、型安全性の保証、クロスプラットフォーム対応の最適化など)が明文化され、各環境における最適な開発スタイルが提供されています。
(1)共通パターン
一貫性の維持と明瞭なコミュニケーション
コーディングスタイル、命名規則、ディレクトリ構造、そしてコミットメッセージの統一が強調されています。
<type>[optional scope]: <description> 形式など、誰が見ても変更意図が明確になる記述ルールが採用されています。
モジュール化とDRY原則
重複コードを排除し、再利用性の高い関数・モジュールに切り出す設計が奨励されています。
RORO(Receive an Object, Return an Object)パターンの活用など、パラメータが増加しても可読性と保守性を損なわない工夫が見られます。
エラー処理と入力検証の徹底
ガード節や早期リターンを用いて、エラー状態を迅速に検知し安全な処理を行う設計が共通しています。
一元化された例外管理や堅牢な入力バリデーションのパターンが、システムの安定稼働に寄与します。
自動化ツール・CI/CDとの連携の強化
統一されたコード規約とコミットメッセージは、静的解析ツールやCI/CDパイプラインとのシームレスな連携を実現し、リリースや品質管理の効率化に貢献しています。
(2)フレームワーク/言語固有のパターン
(2-1)Next.js / React(TypeScript)系ルール
SSR・RSCの活用と「use client」の最小化
サーバーサイドレンダリング(SSR)やサーバーコンポーネント(RSC)を用いて、不要なクライアントサイドレンダリングを削減し、パフォーマンスとSEOを最適化しています。
クライアント側の処理は極力限定し、役割分担を明確化することでデバッグや保守が容易になります。
関数型・宣言的プログラミングの採用
クラスベースではなく、関数型の実装(Reactファンクショナルコンポーネント)を推奨し、シンプルかつテストしやすいコードを実現します。
ディレクトリ構造と命名規則の統一
例としてディレクトリ名を小文字のハイフン区切りにするなど、プロジェクト全体での一貫性を重視します。
(2ー2)Elixir 系ルール
コミットメッセージの明確化
<type>[optional scope]: <description> のようなフォーマットを採用し、変更内容の意図や影響範囲を一目で把握できるようにします。
技術スタックの明示
Phoenix、Docker、PostgreSQL など、使用する主要技術を冒頭で明記することで、チーム間での共通認識を形成し、レビュー時の認識ズレを防ぎます。
関数型プログラミングの強調
Elixir特有のパターンマッチングや不変性など、関数型プログラミングの考え方に基づく実装が推奨されています。
(2-3)Python (FastAPI) 系ルール
型安全性の確保とPydanticの活用
Pythonの型ヒントを徹底し、Pydanticモデルによる入力検証を行うことで、ランタイムエラーや不正なデータ処理を未然に防ぎます。
明確なエラー処理とガード節の採用
エラー発生箇所での早期リターン、ガード節の利用により、コードのネストを浅くし、保守性と可読性を向上させています。
モジュール化による再利用性向上
重複処理を避け、各機能を明確に分離・モジュール化することで、テストや保守がしやすくなるよう設計されています。
(2-4)React Native Expo 系ルール
クロスプラットフォーム開発の最適化
iOSとAndroid両方を視野に入れた開発環境で、統一された命名規則とファイル構造が推奨され、チーム内の作業効率と保守性を向上させます。
Expo固有のAPI利用ガイドライン
ExpoのAPIやReact Native特有の実装パターン(例:スタイリング、コンポーネント設計)を正しく活用するための具体的な指針が示され、効率的な開発と品質向上を支援します。
自動化ツールとの連携
統一されたルールがCI/CDパイプラインと連携することで、変更管理やリリースプロセスの透明性が高まり、全体としてのプロジェクトの信頼性が向上します。
道はまだまだ遠い
世の中の共有されるcursorのルールを見ていると、生成AIに普通の常識を教えるのは結構大変だと思います。
コードをスクラッチから生成したり、1個のバグを修正したりすることは容易になってきています。SWE-bench Verifiedは2023年10月の4%から2025年3月には70%(OpenAIの非公式値)まであがっています。
それでも、1個1個の作業だけではなく、全体のプロジェクト管理を行い、大規模な商用ソフトウェアを維持管理するには、大規模言語モデルの精度だけではなく、さまざまな補完的ノウハウが必要だと思うこの頃です。
むすび
最近はソフトウェアエンジニアの方には人間の仕事は人間が思っているより複雑ですというお話をしています。
ソフトウェア開発には(1)デプロイ、(2)コーディング、(3)デバッグ、(4)常識(プロジェクト内での一貫性)、があります。アラインメント問題を見ていると、最後まで残るのは常識への対応になりそうです。
参考文献
[berman] Matthew Berman: Vibe コーディングの完全なチュートリアルとヒント - カーソル / ウィンドサーフィンhttps://www.youtube.com/watch?v=v7UcVPO4y3c 29m15s 2025年3月
[berman2] https://gist.github.com/mberman84/98fa7d02a2d4c11071bf2bf63faa4713
[patrickjs] PatrickJS: https://github.com/PatrickJS/awesome-cursorrules
[wikipedia] バイブコーディング https://ja.wikipedia.org/wiki/%E3%83%90%E3%82%A4%E3%83%96%E3%82%B3%E3%83%BC%E3%83%87%E3%82%A3%E3%83%B3