← Back to AI Resources
📈

Getting Better Code from AI

Prompt techniques that actually work

The difference between "meh" AI code and actually useful code often comes down to how you ask. These techniques work across ChatGPT, Claude, Copilot, and other AI coding tools.

Techniques That Work

1 Specify the Constraints

Tell the AI your requirements upfront. What frameworks, what style, what restrictions.

❌ Vague

"Write a function to validate email"

✓ Specific

"Write a TypeScript function to validate email. Use regex. Return boolean. No external libraries."

2 Show Input/Output Examples

Examples are worth a thousand words of explanation.

Write a function that formats phone numbers: formatPhone("5551234567") → "(555) 123-4567" formatPhone("555-123-4567") → "(555) 123-4567" formatPhone("1-555-123-4567") → "+1 (555) 123-4567"

3 Ask for Error Handling

AI often generates "happy path" code. Explicitly ask for error handling.

"Write a fetch wrapper that: - Handles network errors - Handles non-200 responses - Has a timeout - Retries on failure (max 3 times) - Returns typed errors"

4 Request Specific Patterns

Name the patterns you want. AI knows them.

"Use the builder pattern" "Implement as a singleton" "Use dependency injection" "Follow repository pattern" "Make it functional, no side effects"

5 Ask for Tests

Ask AI to write tests alongside the code. Good for catching issues.

"Write this function AND unit tests for it. Include edge cases like empty input, null, and very large numbers."

6 Iterate and Refine

Don't accept the first answer. Push back and ask for improvements.

"This works, but: - Can you make it more readable? - The variable names are unclear - Can you extract that logic into a helper function? - Add JSDoc comments"

7 Ask "What Could Go Wrong?"

After getting code, ask AI to critique it.

"Look at the code you just wrote. What are the potential bugs, edge cases, or security issues?"
The meta-tip: Treat AI like a junior developer. It's fast and knows a lot, but needs clear requirements and code review.

Quality Signals to Request

Performance

"Optimize for performance" or "This will run on thousands of items, make it efficient"

Readability

"Prioritize readability over cleverness" or "A junior dev should be able to understand this"

Type Safety

"Use strict TypeScript types, no 'any'" or "Include Zod validation"

Production Ready

"This is for production, not a demo. Include logging, error handling, and monitoring hooks."