Stop Writing Terrible AI Prompts: A Developer's Guide to Prompt Engineering
Your prompts are probably why the AI keeps giving you garbage code. Here's how to fix that and get consistently better results.

I used to think AI models were just inconsistent. One day I'd get brilliant code suggestions, the next day complete nonsense. Turns out the problem wasn't the AI – it was me writing terrible prompts.
After months of integrating Claude and ChatGPT into my development workflow, I've learned that prompt engineering isn't just helpful, it's essential. The difference between a mediocre prompt and a well-crafted one can mean the difference between spending 10 minutes debugging AI-generated code versus getting production-ready snippets on the first try.
The Foundation: Context Is Everything
Here's what I wish someone had told me early on: AI models are incredibly literal. They don't know you're building a React app unless you tell them. They don't know your coding style preferences, your project constraints, or even what programming language you prefer.

I learned this the hard way when I asked Claude to "create a login form" and got back vanilla HTML. Technically correct, but useless for my Next.js project. Now I always start with context:
I'm building a React application using Next.js 14, TypeScript, and Tailwind CSS. I need a login form component that...This simple addition changed everything. Instead of generic solutions, I started getting code that actually fit my stack.
Be Specific About Your Constraints
Vague requests get vague responses. Instead of "make this faster," try:
This React component re-renders too often. I suspect it's the useEffect dependency array. Can you optimize it while keeping the current functionality? I'm using React 18 with strict mode enabled.I've found that mentioning specific constraints upfront saves tons of back-and-forth. Here's my mental checklist:
- What framework/library versions?
- Any performance requirements?
- Accessibility needs?
- Browser support requirements?
- Existing code patterns to follow?
The Power of Examples
This might be the most game-changing technique I've discovered. Instead of describing what you want, show an example of your preferred style:
[paste your existing code]
But for user profiles instead of posts. Follow the same error handling and validation patterns.
`
AI models are pattern matching machines. Give them a pattern to match, and you'll get consistently styled code that fits your project.

Iterative Prompting Beats Perfect First Attempts
I used to try crafting the perfect prompt that would solve everything in one go. That's exhausting and rarely works. Now I embrace iteration:
First prompt: "Create a user authentication hook for React"
Follow-up: "Add error handling for network failures and token refresh logic"
Final: "Can you add TypeScript types and make it compatible with Next.js middleware?"
Each iteration builds on the previous response. It's faster than trying to anticipate every requirement upfront.
Role-Playing for Better Results
This feels weird at first, but it works incredibly well. Instead of just asking for code, give the AI a role:
What specific optimizations would you recommend, and why?
`
I've gotten much more thoughtful, detailed responses using this approach. The AI seems to consider broader architectural concerns instead of just syntax fixes.
Code Review Prompts That Actually Help
Speaking of reviews, here's my go-to template for AI code reviews:
Review this [language] code for:
1. Potential bugs or edge cases
2. Performance improvements
3. Security vulnerabilities[paste code]
I'm particularly concerned about [specific area of concern]. Please explain your reasoning for any suggestions.
`
The key is being specific about what type of review you want. "Review this code" is too broad and usually results in generic style suggestions.

Debugging Prompts That Work
When something's broken, don't just paste your code and say "fix this." Context about the problem is crucial:
[code and error details]
I suspect it's a cleanup issue, but I'm not sure where.
`
Include the error message, when it occurs, what you've already tried, and your hypothesis about the cause. This helps the AI focus on likely solutions instead of suggesting generic debugging steps.
Testing and Documentation Prompts
I've found AI particularly helpful for generating test cases and documentation, but you need to be specific:
Write Jest unit tests for this utility function. Focus on edge cases like empty inputs, malformed data, and boundary values. Use the existing test style from this example: [paste example test]For documentation:
Create API documentation for this Express route. Include request/response examples, possible error codes, and any authentication requirements. Format it for our existing OpenAPI spec.Common Mistakes I See (And Made)
Too much code at once: AI models have context limits. Break large codebases into focused chunks.
No version specificity: "React hooks" could mean React 16.8 or React 18. Versions matter.
Asking for explanations after the fact: If you want to understand the solution, ask for explanations upfront: "Explain your approach as you code."
Ignoring follow-up questions: When AI asks for clarification, don't ignore it. Those questions usually lead to better solutions.
Practical Takeaways
- Always provide your tech stack and versions in the first prompt
- Use examples of your existing code style as references
- Be specific about constraints and requirements
- Embrace iterative prompting instead of trying to be perfect upfront
- Give the AI a role or persona for more thoughtful responses
- Include error messages and context when debugging
- Ask for explanations when you want to learn, not just get solutions
The goal isn't to replace your thinking – it's to augment it. Good prompts help you collaborate with AI tools instead of fighting them. I've gone from seeing AI as an unreliable code generator to treating it as a knowledgeable pair programming partner.
What's your biggest prompt engineering challenge? I'm curious if these techniques work as well for others as they have for me.

Ibrahim Lawal
Full-Stack Developer & AI Integration Specialist. Building AI-powered products that solve real problems.
View Portfolio