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 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
📚 相关资源
❓ 常见问题
关于本章主题最常被搜索的问题,点击展开答案
让 AI 写代码,Prompt 应该写哪四件事?
目标(要做什么功能)、上下文(语言版本、框架、约束、已有代码)、输入(数据结构、样例、边界)、输出(格式、文件位置、函数签名)。这四块一缺,模型就开始臆测——TypeScript 5 的代码可能写成 ES5、Node.js 20 的写成 Node 14,编译直接报错。
为什么生成的代码经常跑不通?
本章列了四个常见原因:上下文不够(没说依赖版本)、边界没列(空数组、null、异常输入没覆盖)、风格不一致(lint 规则没给)、输出格式没钉(多了 markdown 代码块解释)。修法是把这些都写进 Prompt,比如「Node.js 20,不能引入新依赖,只输出代码」,本章修 bug 示例就是这个写法。
用 AI 写测试用例靠谱吗?要怎么 Prompt?
靠谱但要给约束。本章 Jest 示例是好模板:写明 testing 框架(Jest / Vitest / pytest)、覆盖范围(正常输入、空数组、异常输入)、目标函数代码完整粘贴。最容易翻车的点是模型只测 happy path——必须显式列 edge cases,否则边界 bug 全漏。
代码 Prompt 输出风格不一致怎么办?
把团队 lint 规则或一段已有代码作为 style anchor 放进 Prompt 上下文,例如「参考下方风格,使用同样的命名、缩进、import 顺序」,再粘一段团队代码。比单纯说「保持现有风格」精准得多。Cursor / Claude Code 这类 IDE agent 直接读代码库会更稳。
改 bug 用 AI 时,怎么避免它把无关代码也重写一遍?
Prompt 里写「保持现有 API 不变」「只修问题相关代码,其他保持原样」「输出 diff 而不是整个文件」。本章修 `calcTotal` 精度 bug 的示例就是这种做法——只列问题(amount=0 报错、精度异常)、给约束(不引入新依赖)、限制说明(3 条以内),模型不敢扩散修改。