Prompt Engineering 指南:在 AI 编写了我们 75% 的代码之前,没人需要它
Source: Dev.to
Source: …
AI‑Assisted Coding: A Prompting Framework that Works
六个月前,我们的团队开始使用 AI 编码工具。第一周生产力提升了 30 %,但代码质量下降,bug 报告增多,PR 审核时间翻倍。问题不在于 AI 本身,而在于我们如何向它发出提示。
下面是我们经过六个月反复试验后提炼出的提示框架。它适用于 Cursor、GitHub Copilot、Claude Code、ChatGPT 等模型。
1. 为什么大多数提示会失败
大多数开发者和模型的对话方式就像在和一个初级同事交谈:
“构建一个登录页面。”
模型会乖乖返回一个页面,但 没有 错误处理、可访问性、加载状态、限流或现代化样式——往往还使用了已废弃的库。
如果提示模糊,输出就会通用。
2. 四要素提示模板
每个提示都应包含 全部 以下要素:
| # | 要素 | 应包含的内容 |
|---|---|---|
| 1 | Context(上下文) – 已有的代码是什么?技术栈是什么?代码库使用了哪些架构模式? | |
| 2 | Goal(目标) – 你想实现的具体任务。 | |
| 3 | Non‑functional requirements(非功能性需求) – 错误处理、测试、性能、可访问性、安全等。 | |
| 4 | Negative constraints(负向约束) – AI 必须避免的内容(反模式、禁用库等)。 |
3. 示例提示(正向 + 负向)
Prompt
Add search to the products page
完整提示(使用模板)
Situation:
- Next.js 14 app with App Router
- PostgreSQL via Prisma
- Tailwind CSS
- Existing products page at app/products/page.tsx (server component) that fetches all products
Goal:
- Implement search so users can type a query and see filtered results.
- Search must cover product name **and** description.
Requirements:
- Debounced input (300 ms) to avoid excessive queries.
- URL‑based search params (shareable/bookmarkable).
- Loading state while searching.
- “No results” state with a clear message.
- Minimum 3 characters before triggering a search.
- Graceful handling of Prisma errors (show error state, don’t crash).
Constraints:
- No client‑side filtering (dataset > 50 k products).
- No new dependency for search – use Prisma full‑text search.
- Do **not** use `useEffect` for data fetching (use server components + `searchParams`).
- Follow the existing code style (see other pages for reference).
结果: 输出的是可直接投产的代码,而不是快速演示。
4. 负向提示 – 明确告诉 AI 不要 做什么
“实现用户注册接口。”
不要:
- 使用 any 类型
- 静默捕获错误(每个 catch 必须记录日志并重新抛出或返回错误响应)
- 跳过输入校验
- 在响应中返回密码哈希
- 使用同步 bcrypt(请使用异步版本)
- 创建数据库表(表已存在,见
schema.prisma)
因为模型是基于数百万代码片段训练的——其中不少包含不良实践——负向列表可以把它们从常见陷阱中拦截出来。
5. 向模型展示你的代码风格
// ── example endpoint ──
// (paste an existing endpoint)
Following this exact pattern (error handling, response format, validation approach, naming convention), implement a new endpoint for .
提供具体的代码示例要比用文字描述风格更可靠;模型会直接逆向工程出相应的模式。
6. 逐步构建解决方案
| 步骤 | Prompt |
|---|---|
| 1 | “Create the database query for searching products by name, with pagination. Only the query, not the API endpoint.” |
| 2 | “Now wrap this in an API endpoint following our existing pattern. Add input validation with Zod.” |
| 3 | “Add error handling. What happens if Prisma throws? What if the search param is empty?” |
| 4 | “Write tests. Cover: successful search, empty results, invalid input, database error.” |
|
每一步都建立在前一步的基础上,让你能够提前验证正确性。如果第 3 步失败,你只需要重新做第 3 步——而不是整个实现。
7. Self‑Review Prompt
在代码生成后,要求模型审计自己的工作:
Review the code you just generated. Check for:
1. Security vulnerabilities (injection, auth bypass, data exposure)
2. Error‑handling gaps (uncaught exceptions)
3. Edge cases (empty input, concurrent access, very large input)
4. Performance issues (N+1 queries, missing indexes, unnecessary computation)
List every issue you find with the specific line number.
这可以捕获 60‑70 % 的 AI 引入的问题——在人工审查之前进行一次廉价的首次检查。
8. Explanation Prompt (When You’re Unsure)
Explain every line of this code. For each line:
1. What it does
2. Why it’s necessary
3. What would happen if it were removed
If any line is unnecessary, say so.
如果模型无法为某行提供理由,该行很可能是多余或有风险的。
9. Formal Rules (What We Enforced After 6 Months)
-
Every line of AI‑generated code receives the same PR review as human‑written code.
The AI is the author; the developer is the responsible party. -
If you can’t explain every line, don’t commit it. Use the Explanation Prompt.
-
Architectural decisions come from the team, not the prompt. AI writes the code; humans decide what to write and why.
-
Treat AI‑generated code as having different failure modes: syntactically perfect but semantically wrong. Tests are the safety net.
-
Document complex generated code with the original prompt:
// Generated with prompt: // "Implement rate limiting middleware using sliding‑window algorithm. // Max 100 requests per minute per API key. Use Redis for storage. // Return 429 with Retry‑After header." // Reviewed by: @jane on 2026‑02‑15This helps future developers understand intent and regenerate if needed.
10. Measured Impact
| 指标 | 引入框架前 | 引入框架后 |
|---|---|---|
| 代码产出 | +30 % | +25 %(略低,因为提示需要时间) |
| 缺陷率 | +20 % | –10 %(比 AI 引入前基线少 10 %) |
| 净生产力提升 | ~15 % | ~35 % |
| 提示成本 | – | ~60 秒/次(对降低缺陷的收益而言值得) |
TL;DR
- 提供包含上下文、目标、非功能性需求以及明确的“禁止”列表的提示。
- 向模型展示你的代码风格示例。
- 小步迭代,并让模型审查自己的输出。
- 当你不完全理解代码时,要求逐行解释。
- 将 AI 生成的代码视为其他任何贡献:完整的 PR 审查、测试和文档。
遵循此框架将原本约 15 % 的生产力提升转化为 ~35 % 的增长,并显著提升代码质量。 🚀
讨论提示
数学很清晰。
你的团队如何使用 AI 编码工具?
你们制定了团队标准吗?我很想听听哪些方法有效——以及哪些失败了。
评论开放。
AI 编码提示模板包
如果你想要一个现成的提示模板和光标规则文件集合,我已经整理了一个 AI Coding Prompt Templates Pack,其中包括:
- 50+ 提示模板
- 8 个光标规则文件
- 用于 Claude Code 和 Copilot 的工作流模板
欢迎查看并告诉我你的想法!