Lesson 29 of 1570
Prompting for Code Is Different From Prompting for Prose
A prompt that writes a poem is not the same as a prompt that ships working code. Code has hidden standards. You need to make them explicit.
Lesson map
What this lesson covers
Learning path
The main moves in order
- 1Prose Forgives. Code Does Not.
- 2code prompts
- 3constraints
- 4I/O examples
Concept cluster
Terms to connect while reading
Section 1
Prose Forgives. Code Does Not.
A poem about loneliness can be five lines or fifty and still work. A function that adds two numbers has to return the exact right number, in the exact right type, or the program breaks. Prompts for code have to be more precise, because code is less forgiving.
The four things every code prompt needs
- 1Language and version (Python 3.12, TypeScript, etc.)
- 2Inputs: types, shapes, and example values
- 3Outputs: what to return, including edge cases
- 4Constraints: performance, dependencies, style
A weak prompt vs. a strong prompt
Compare the options
| Weak | Strong |
|---|---|
| Write a function to sort things | Write a TypeScript function sortByDate(items: {id: string, created: Date}[]) that returns the array sorted oldest first. Do not mutate the input. |
| Make me an API | Write a Node.js Express route POST /signup that takes {email, password}, hashes the password with bcrypt, and returns 201 with {id, email}. |
| Fix this bug | This function returns NaN when items is empty. Expected behavior is to return 0. Fix the bug and keep the signature. |
The example-driven prompt template
A prompt you could paste into any AI coding tool and get a reliable, testable function back.
Task: Write a function `slugify(text: string): string`.
Language: TypeScript.
Behavior:
- Lowercase the input.
- Replace spaces with hyphens.
- Strip characters that are not a-z, 0-9, or hyphen.
- Collapse multiple hyphens into one.
Example:
slugify("Hello, World!") === "hello-world"
slugify(" A B ") === "a-b"
Constraints:
- Pure function.
- No external dependencies.What to leave out
- Do not ask the AI to be creative unless you actually want variety
- Do not pile on so many rules that the task becomes contradictory
- Do not ask for best practices vaguely — name the practice
“The best programmers write the spec before the code, whether or not an AI is helping.”
Key terms in this lesson
The big idea: code is a contract, so a code prompt must be a contract too. Inputs, outputs, examples, constraints. Nail those, and the AI stops guessing and starts delivering.
End-of-lesson quiz
Check what stuck
15 questions · Score saves to your progress.
Tutor
Curious about “Prompting for Code Is Different From Prompting for Prose”?
Ask anything about this lesson. I’ll answer using just what you’re reading — short, friendly, grounded.
Progress saved locally in this browser. Sign in to sync across devices.
Related lessons
Keep going
Builders · 25 min
What Does AI-Assisted Coding Even Mean?
AI-assisted coding is not magic and not cheating. It is a new way of working where a model drafts, you decide. Let's draw a map before we start building.
Builders · 30 min
Your First Copilot-Style Completion
Let's actually feel what autocomplete is like. Write a comment, pause, and watch a full function appear. Then learn what to do next.
Builders · 35 min
When AI Writes Buggy Code — How to Read It Critically
The AI will hand you code that looks right but isn't. Here are the most common bugs and the habits that catch them before they bite.
