Coding Prompts
Code generation prompts (overview)
Code generation prompts (overview)
A reusable prompt, example set, evaluation record or safety rule with its task boundary preserved.
Run at least one representative case, inspect the result and record what still requires human review.
This section collects code generation prompts. The point isn't "let AI write all the code" -- it's about turning prompts into a repeatable interface (clear input + constraints + output format) for better control and reusability.
Use Cases
- Quickly generate boilerplate code or scaffolding
- Turn natural language requirements into executable code
- Fix bugs, add edge case handling, or add logging
- Generate test cases, align interface formats
Core Structure (include these every time)
- Goal: What functionality to build
- Context: Language version, framework, constraints, existing code
- Input: Data structures, examples, edge cases
- Output: Format, file location, function signature
Turn this chapter's knowledge into practical skills
Enter the interactive lab and practice Prompt with real tasks. Get started in 10 minutes.
Prompt Template (General)
You are a senior engineer. Implement the following:
Goal:
- {{WHAT_TO_BUILD}}
Context:
- Language: {{LANG_VERSION}}
- Framework: {{FRAMEWORK}}
- Style: {{STYLE_OR_GUIDE}}
- Constraints: {{CONSTRAINTS}}
Input:
{{INPUT_SPEC}}
Output:
- Code only
- Must include {{FUNCTION_NAME}}
- Keep existing API unchanged
Example 1: Generate a Function
You are a senior frontend engineer. Implement a function in TypeScript.
Goal:
- Group a user list by age
Context:
- Language: TypeScript 5
- Constraints: Pure function, don't mutate the original array
Input:
type User = { id: string; name: string; age: number }
const users: User[] = [...]
Output:
- Function name: groupByAge
- Returns Record<number, User[]>
- Code only
Example 2: Fix a Bug
You are a senior backend engineer. Fix the code below and explain why.
Problem:
- Throws an error when amount is 0
- Precision issue: 0.30000000000004
Context:
- Node.js 20
- Cannot add new dependencies
Code:
function calcTotal(items) {
return items.reduce((sum, item) => sum + item.amount, 0);
}
Output:
- Fixed code
- 3 or fewer lines of explanation
Example 3: Generate Test Cases
You are a senior test engineer. Write unit tests for the function below.
Context:
- Testing: Jest
- Cover normal input, empty array, invalid input
Code:
export function normalizeEmail(email: string) {
return email.trim().toLowerCase();
}
Output:
- Jest test code
Common Problems & Fixes
- Unstable output: Add length/format constraints, fix field order
- Missing edge cases: Explicitly list edge cases, require coverage
- Code doesn't run: Provide more complete context and dependency versions
- Inconsistent style: Specify lint rules or existing style guide
Index
📚 Related resources
❓ Common questions
Open a question to review the practical answer.
When asking AI to write code, what four things must my prompt cover?
Goal (what feature to build), context (language version, framework, constraints, existing code), input (data shape, samples, edges), output (format, file location, function signature). Drop any of those and the model starts guessing — TypeScript 5 code may come back as ES5, Node.js 20 may come back as Node 14, and the compiler trips immediately.
Why does AI-generated code so often fail to run?
The chapter lists four usual culprits: not enough context (no dependency versions), missing edges (no coverage of empty array, null, or bad input), inconsistent style (no lint rules), and loose output format (extra markdown chatter). Fix by stating all of them in the prompt — e.g. `Node.js 20, no new dependencies, code only` — exactly like the bug-fix example in this chapter.
Is AI any good at writing test cases, and how should I prompt it?
Yes, with constraints. The Jest example in this chapter is a solid template: state the test framework (Jest / Vitest / pytest), the coverage scope (happy path, empty array, bad input), and paste the target function in full. The classic failure is the model writing happy-path tests only, so you must list the edge cases explicitly or boundary bugs go unnoticed.
What if AI-generated code doesn't match our coding style?
Put your team lint rules or an existing snippet into the prompt as a style anchor — `use the naming, indentation and import order shown below`, then paste real team code. Far more accurate than just saying `keep existing style`. IDE agents like Cursor or Claude Code that read the codebase directly do this even better.
When fixing bugs with AI, how do I stop it from rewriting unrelated code?
Tell the prompt `keep existing APIs unchanged`, `touch only the buggy code`, and `output a diff, not the whole file`. The `calcTotal` precision bug example in this chapter does exactly that — list the symptoms (`amount=0` throws, precision artefact), the constraints (no new dependencies), and a comment cap (under 3 lines), so the model dares not sprawl the edit.