AI Starter Package
3 of 8 · 35 min

Effective Prompting for Code

Why Prompts Matter

The same question phrased two different ways produces dramatically different results. Vague prompts get vague code. Specific prompts get production-ready implementations. This lesson teaches you seven patterns that consistently produce high-quality output.

The Scaffold Pattern

Use when: Building something from scratch.

"Build a REST API with Express and TypeScript. Include user CRUD endpoints, input validation with Zod, and error handling middleware."

Include the tech stack, key features, and constraints. The more specific, the better the scaffold.

The Feature Pattern

Use when: Adding to existing code.

"Add pagination to the /api/articles endpoint. Support page and limit query params, default to 20 items per page, return total count in the response."

The Fix Pattern

Use when: Debugging errors.

"When I click the submit button, I get a 500 error. The console shows 'Cannot read property id of undefined' in the UserForm component. Fix this."

Always include: what you did, what happened, and what you expected.

The Refactor Pattern

Use when: Improving existing code without changing behavior.

"Refactor the auth middleware to use early returns instead of nested if-else. Keep the same behavior but make it easier to read."

The Test Pattern

Use when: Adding test coverage.

"Write tests for the checkout flow covering: successful purchase, expired card, insufficient funds, and empty cart. Use Jest and React Testing Library."

The Review and Plan Patterns

Review: "Review the auth module for security vulnerabilities, missing error handling, and race conditions." Great for catching bugs before they ship.

Plan: "Plan how to migrate from REST to GraphQL. List the steps, risks, and estimated effort before writing any code." Forces Claude to think before acting.

Bad vs Good Prompts

  • Bad: "Fix my code" → Good: "The login form submits but the JWT token is not being stored in cookies. Check auth.ts and the login API route."
  • Bad: "Make it better" → Good: "Reduce the bundle size of the dashboard page. Check for unnecessary imports and large dependencies."
  • Bad: "Add tests" → Good: "Write unit tests for the pricing calculator covering free tier, pro tier, and annual discount logic."

The pattern is always the same: what you want, where it lives, and what success looks like.

Practice Exercise

Take one of these patterns and use it right now on your own project. Pick the pattern that matches your current need:

  • Have a bug? Use the Fix Pattern with the exact error message.
  • Starting fresh? Use the Scaffold Pattern with your preferred stack.
  • Code getting messy? Use the Refactor Pattern on the worst file.

Notice the difference between a vague instruction and a specific one. The quality gap is immediate and obvious.