Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Prompt Engineering

Prompt Engineering

A Prompt is the only communication channel between you and AI; for the same requirement, different Prompts can differ in output quality by 10x.

The five elements of a good Prompt:

Element Core question What happens if it's missing
Goals What to do? AI guesses your intent and may go off track
Constraint What constraints? AI uses a tech stack you're unfamiliar with and the code can't be integrated
Input What data does it receive? AI assumes an input format and it errors at runtime
Output What result does it produce? AI gives code but no usage instructions, so you don't know how to run it
Acceptance criteria What counts as passing? AI finishes but you don't know whether it counts as done


Element one: goal - state clearly what to do

A bad way to write it



帮我写一个表单

Problem: what form? Login? Registration? Search? How many fields?

A good way to write it



Help me write a user registration form, including the following fields:

用户名、邮箱、密码、确认密码

An even better way to write it



Help me write a user registration form, including the following fields:

- 用户名(文本框,3-20 个字符)

- 邮箱(文本框,需验证格式)

- 密码(密码框,至少 6 位,包含数字和字母)

- 确认密码(密码框,需与密码一致)

Key tip: break "what to do" into concrete components. AI understands concrete things far better than vague concepts.


Element two: constraints - draw the boundaries

Constraints tell AI what not to do and what it must use. Without constraints, AI improvises freely and the code won't integrate into your project.

Common types of constraints

技术栈约束:

- 使用 React 18 + TypeScript

- 不引入第三方 UI 库(自己写 CSS)

- 使用 Vite 作为构建工具



Code style constraints:

- 使用函数组件和 Hooks,不用 Class 组件

- 每个文件不超过 150 行

- 所有 props 都要有 TypeScript 类型定义



Output constraints:

- 所有代码放在一个文件里

- 给出完整的 import 语句

- 关键逻辑加上注释

The more constraints you write, the more controllable AI's output. But don't write too many at once - start with 3-5 core constraints and add more based on the results.


Element three: input - define the data format

Tell AI what the input data looks like and it can write more accurate code.

A bad way to write it



写一个函数处理用户数据

A good way to write it



Write a function formatUser(user) that accepts a user object:

{

  name: string,       // 用户名,例如 "张三"

  age: number,        // 年龄,例如 25

  email: string,      // 邮箱,例如 "zhangsan@example.com"

  createdAt: string   // 注册时间,ISO 格式,例如 "2026-06-28T10:00:00Z"

}

返回格式化的用户信息字符串。

Key tip: attach an example value to every field. AI understands examples far better than abstract type descriptions.


Element four: output - specify the result form

Tell AI the output form you expect - code? an explanation? revision suggestions?

Common output forms

Output form Applicable scenarios Prompt example
Complete code New feature, new file Give me the complete code, including all imports
Code modification Modify existing code Only output the changed parts, in diff format
Explanation Understand code Explain line by line what this code does
Solution suggestions Architecture decisions Give two implementation options and compare their pros and cons
Troubleshooting Debugging Analyze the cause of this error and give the fix steps

Element five: acceptance criteria - define "done"

Acceptance criteria are the most easily overlooked element, yet they determine whether you can judge the task as complete.

Examples of good acceptance criteria



Acceptance criteria:

- 用户名少于 3 个字符时,显示红色提示"用户名至少 3 个字符"

- 密码少于 6 位时,显示红色提示"密码至少 6 位"

- 两次密码不一致时,显示红色提示"两次密码不一致"

- 所有字段为空时点击提交,每个字段下方显示对应的错误提示

- 所有字段正确填写后点击提交,控制台打印表单数据,不发送请求

Key tip: make acceptance criteria as concrete as "input X, see Y", using the phrasing "when ... , it should ...".


Prompt template

Below is the Prompt template used throughout the rest of this course; you can apply it directly:



## 目标

[一句话描述要做什么]



## 约束

- [技术栈限制]

- [代码风格要求]

- [输出格式要求]



## 输入

[输入数据的结构和示例]



## 输出

[期望的输出形式]



## Acceptance Criteria

- [具体可验证的标准 1]

- [具体可验证的标准 2]

- [具体可验证的标准 3]


Common Prompt mistakes and their fixes

Error Problem Fix
Make me an e-commerce site Too vague; AI doesn't know where to start Make the product listing page for the store, showing product image, name, and price, 3 products per row
Implement it the best way AI doesn't know what "best" means to you Use React.memo and useCallback to optimize rendering performance
This code has a problem, help me fix it Doesn't say where the problem is After clicking the delete button the list didn't update and the console errors "id is undefined", help me fix it
Add a feature Doesn't say which file or what position to add it to In src/components/Header.tsx, on the right side of the nav bar, add a user avatar dropdown menu

Hands-on: one requirement, three Prompts compared

Requirement: make a today's to-do list.

Version A: one-sentence Prompt



帮我做一个待办列表

AI output: maybe a simple HTML page, maybe it used React, maybe Vue - you don't know what you'll get.

Version B: Prompt with constraints added



帮我做一个待办列表,用 HTML + CSS + 原生 JavaScript,

可以添加和删除任务。

AI output: a usable to-do list, but the styling may be crude and empty input may not be handled.

Version C: complete five-element Prompt



## 目标

做一个"今日待办"列表组件。



## 约束

- 使用 HTML + CSS + 原生 JavaScript,单文件

- 不使用任何第三方库

- 移动端友好的响应式布局



## 输入

- 用户在输入框中输入待办事项文字

- 用户点击"添加"按钮或按 Enter 键



## 输出

- 一个完整的 HTML 文件,可以直接在浏览器打开

- 输入框在顶部,已添加的待办事项在下方列表显示

- 每个待办事项右侧有删除按钮

- 底部显示"共 X 项,已完成 Y 项"



## Acceptance Criteria

- 输入空内容点击添加时,显示提示"请输入待办事项"

- 添加成功后,输入框清空

- 点击待办事项文字,切换完成/未完成状态(加删除线)

- 点击删除按钮,该项消失

- 统计数据实时更新

- 刷新页面后数据保留(使用 localStorage)

AI output: a fully featured to-do list with data persistence, statistics, and error handling.

Comparing the three versions' outputs, you'll intuitively feel that the quality ceiling of the Prompt is the quality ceiling of AI's output.


Iterative development in practice

Vibe Coding's core workflow is not giving one Prompt and getting perfect code once, but a loop of Prompt -> verify -> feedback -> verify again.

The five stages of iterative development

Make it run Make it usable Fix the Bugs Polish the details Add tests and documentation First make it exist, then make it good, finally make it polished

Hands-on project: build a "Programming Terms Dictionary"

This project builds a searchable programming terms dictionary containing term name, explanation, and category.

Follow the five iterations below and actually perform each one.

Round one: make it run

Prompt:



Create a programming terminology dictionary webpage, requirements:

- 使用 HTML + CSS + 原生 JavaScript,单文件

- 显示一个术语列表,每条术语包含名称和简短解释

- 预置 10 个常见编程术语(HTML、CSS、JavaScript、API、变量、函数、循环、数组、对象、Git)

- 顶部有一个搜索框,输入关键词可以过滤术语

- 深色背景,卡片式布局

Save the AI-generated code as dict.html, and open it in the browser.

The goal of round one isn't perfection but getting it to run. A running-but-imperfect version is 100x better than an imagined perfect version that isn't written yet.

Round two: make it usable

Prompt:



Based on the code from the previous round, make the following improvements:

1. 每个术语卡片加上分类标签(如"前端"、"JavaScript"、"通用"等)

2. 搜索时同时匹配术语名称和分类标签

3. 搜索结果为空时,显示"没有找到相关术语"而不是空白

4. 添加一个"随机学习"按钮,点击后随机展示一个术语

5. 术语列表按字母顺序排列

Verify: are category tags shown? Does search match categories? Is there a hint for empty results? Does the random button work?

Feedback: check item by item and tell AI anything that isn't satisfied.

Round three: fix the Bugs

In this round you actively hunt for Bugs.

Test the following scenarios:

Enter special characters in the search box (such as <, >, /).

Enter spaces in the search box (leading/trailing spaces).

Click the random button rapidly and repeatedly.

Enter uppercase letters in the search box (the original data may be lowercase).

Delete all the terms (if deletion is supported).

For each Bug you find, describe it to AI in a structured way:



当我在搜索框中输入大写 "HTML" 时,没有匹配到任何结果。

预期:不区分大小写,输入 "html" 和 "HTML" 都能匹配到。

实际:只有输入小写 "html" 才能匹配。

请修复这个大小写敏感的问题。

Round four: polish the details

Prompt:



Based on the previous code, optimize the following details:

1. 搜索时添加防抖(输入停止 300ms 后再搜索,避免频繁过滤)

2. 卡片 hover 时有一个微小的上浮动画

3. 搜索结果高亮匹配的关键词(用黄色背景标记)

4. 添加一个"已学 X 个"的计数器,点击术语卡片上的"已学"按钮 +1

5. 已学过的术语显示一个勾的标记

6. 用 localStorage 保存已学记录

Verify: are the animations smooth? Is the highlighting correct? Do learned records survive a refresh?

Round five: add tests and documentation

Prompt:



Based on the previous code:

1. 在代码顶部用注释写出功能说明和用法

2. 为搜索过滤函数写注释,说明它的输入、输出和逻辑

3. 如果代码中有复杂的逻辑,拆分成更小的函数,每个函数只做一件事

4. 在文件中添加数据来源说明(术语数据来自哪里)


Key habits during iteration

Change only one thing at a time

If AI changes many things at once but you only wanted one, say so clearly:



只修改搜索框的大小写问题,不要改动其他任何代码。

Run verification immediately after each iteration

Don't pile up several changes and verify them together - run it once per round of changes to make sure no new problems were introduced.

Keep the version from every round

Use Git to commit each round's result:



git add dict.html && git commit -m "第一轮:基本功能能跑"

git add dict.html && git commit -m "第二轮:添加分类和随机功能"

git add dict.html && git commit -m "第三轮:修复大小写和特殊字符 bug"

git add dict.html && git commit -m "第四轮:添加动画和已学记录"

git add dict.html && git commit -m "第五轮:添加注释和文档"

That way you can return to any version anytime and compare the changes from each round.

Git is Vibe Coding's safety net. When you have AI make major changes, first commit the current version, and roll back if it breaks.