The quality of your AI-generated code depends directly on the quality of your prompts. Prompt engineering for code follows its own rules — technical precision, context provision, and iterative refinement.
A good prompt contains five elements:
Bad:
"Create a login function"
Good:
"Create a handleLogin server action in src/app/actions/auth.ts. Use Supabase Auth with signInWithPassword. Validate email and password with Zod. On success redirect to /dashboard, on error return a typed error message. Follow the pattern in @file:src/app/actions/signup.ts"
Instead of generating an entire feature in one prompt — build it step by step:
Pattern-based refactoring:
"Refactor @file:src/components/UserList.tsx — replace the useEffect + useState for data loading with useSWR. Keep the existing error handling."
Performance refactoring:
"Optimize the render performance of @file:src/components/Dashboard.tsx — identify unnecessary re-renders and apply React.memo, useMemo, and useCallback appropriately."
Architecture refactoring:
"Extract the business logic from @file:src/components/OrderForm.tsx into a custom hook useOrderForm. The component should only render UI."
Error message + context:
"I'm getting this error: [paste error message]. The error occurs in @file:src/lib/api.ts line 42. The relevant function uses @file:src/types/api.ts. What's the cause?"
Reproducible description:
"When I click 'Save', the form in @file:src/components/EditProfile.tsx submits, but the data in Supabase doesn't update. The Network tab shows a 200 status. No error in the console."
Unit tests:
"Write Vitest unit tests for @file:src/lib/utils/pricing.ts. Test all functions with valid inputs, edge cases (0, negative numbers, undefined), and error cases. Use describe/it blocks with German descriptions."
Integration tests:
"Write an integration test for the signup flow: fill form → server action → create Supabase user → redirect. Mock Supabase with vi.mock."
"Create JSDoc comments for all exported functions in @file:src/lib/billing/stripe.ts. Describe parameters, return values, and possible errors. Add @example blocks."
"Analyze the current git diff and write a Conventional Commit message. Format: type(scope): description. Body with the key changes."
Golden rule: The more context you give the AI, the better the output. Always reference existing files, types, and patterns — the AI should write code that fits into your codebase, not generic code.