How do I write good prompts for AI coding assistants?
Key elements of a good prompt
A good prompt tells the AI exactly what you want, with enough context to avoid guessing. Start by stating the goal: what feature or fix you need. Then specify the tech stack (e.g., React 18, Next.js 14, TypeScript). Describe the expected behavior, inputs, and outputs. Mention any constraints like performance, accessibility, or styling libraries. If you have existing code, include the relevant snippet. The more precise you are, the less back-and-forth you'll need.
- State the goal clearly: 'Create a login form' vs. 'Build a React component'
- Specify the stack: framework, language, and versions
- Describe inputs, outputs, and edge cases
- Include constraints: styling, accessibility, performance
- Provide existing code or file structure when relevant
Iterate and refine
Treat the first response as a draft. If the code isn't right, point out what's wrong and ask for a fix. Use follow-up prompts to add features or refactor. For complex tasks, break them into smaller prompts: first ask for the component structure, then for the logic, then for styling. This step-by-step approach reduces errors and makes it easier to spot issues. Also, ask the AI to explain its reasoning or suggest alternatives.
Common mistakes
- Writing vague prompts like 'make a website' and expecting perfect results.
- Not mentioning the framework or version, leading to outdated code.
- Trying to get everything in one prompt instead of breaking it down.
