Lesson 3 of 6 · 12 min
Prompting for Code That Works
Give AI assistants the context they need, and get code you can actually ship.
Context in, quality out
AI assistants can only work with what you tell them. A good coding prompt includes:
- Goal: what should the code do?
- Context: language, framework, versions, existing code it must fit into
- Constraints: no new libraries, must handle empty input, performance limits
- Interface: function name, inputs, outputs, examples
- Definition of done: "include tests", "explain any trade-offs"
Weak
write a function to validate emails
Strong
Write a JavaScript function isValidEmail(email) that returns true/false.
- Must reject empty strings, missing @, and missing domain
- Don't use a regex longer than one line; readability matters
- Include 6 test cases using the format test("name", () => expect(...).toBe(...))
Iterate in small steps
Don't ask for a whole app in one prompt. Ask for one piece, run it, review it, then ask for the next. This is the same "small steps" habit from your first lesson.
When it's wrong
Give precise feedback: the failing input, the actual output, the expected output and the error message. Vague feedback ("it's broken") gets vague fixes.
Professional rules
- Never paste secrets (API keys, passwords, customer data) into AI tools, and follow your company's AI policy.
- Check licenses and sources for large generated code blocks.
- You own it: if you can't explain a line, don't commit it.
Practice: write a precise prompt for one of your exercises in any AI assistant, then compare its output to your own solution and run it against the tests in the Playground.
✅ Check your understanding
Which prompt will most likely produce useful code?
The AI's code fails a test. What's the most effective next prompt?
What should you never paste into a public AI tool?
Create a free account to track your progress.