Coding Prompts
Code generation prompts (overview)
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
Prompt Lab
View NowTurn 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