Identifying and Correcting Errors · 识别与更正错误
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| bug/bʌɡ/ | 缺陷 | quē xiàn |
| debugging/ˈdiːbʌɡɪŋ/ | 调试 | tiáo shì |
| syntax error/ˈsɪntæks ˈerə/ | 语法错误 | yǔ fǎ cuò wù |
| logic error/ˈlɒdʒɪk ˈerə/ | 逻辑错误 | luó jí cuò wù |
| runtime error/ˈrʌntaɪm ˈerə/ | 运行时错误 | yùn xíng shí cuò wù |
| overflow error/ˌəʊvəˈfləʊ ˈerə/ | 溢出错误 | yì chū cuò wù |
| edge case/edʒ keɪs/ | 边界情况 | biān jiè qíng kuàng |
| expected output/ekˈspektɪd ˈaʊtpʊt/ | 预期输出 | yù qī shū chū |
Every program has bugs
- No matter how careful the team is, programs contain mistakes.
- A mistake in a program is a bug 缺陷.
- Finding and fixing bugs is called debugging 调试.
- Skilled debugging is one of the most valuable abilities a programmer can build.
每个程序都有缺陷
- 无论团队多么小心,程序都含有错误。
- 程序中的错误叫缺陷(bug)。
- 查找并修复缺陷叫调试(debugging)。
- 熟练的调试是程序员能培养的最有价值的能力之一。
Which kind of error? · 哪种错误?
A syntax error stops the program running; a logic error runs but gives the wrong answer; a runtime error crashes it partway; an overflow error exceeds storage. · 语法错误阻止程序运行;逻辑错误能运行但给出错误答案;运行时错误中途崩溃;溢出错误超过存储。
The work of finding and fixing mistakes in a program is called . · 在程序中查找并修复错误的工作叫。
A mistake is a bug; fixing bugs is debugging. · 错误是缺陷;修复缺陷是调试。
A missing bracket that stops the program from running is a: · 一个让程序无法运行的缺失括号是:
Breaking the language's rules is a syntax error. · 违反语言的规则是语法错误。
Why are logic errors often the hardest to find? · 为什么逻辑错误常常最难找到?
Nothing obviously breaks, so you must compare output to what you expected. · 没有明显的崩溃,所以你必须把输出与预期比较。
The main kinds of error
- A syntax error 语法错误 breaks the language's rules (a missing bracket) — the program refuses to run.
- A logic error 逻辑错误 runs but gives the wrong result, because the instructions do not match the plan.
- A runtime error 运行时错误 crashes the program while it runs — like dividing by zero.
- An overflow error 溢出错误 happens when a value grows too large for its storage space.
主要的错误种类
- 语法错误(syntax error)违反语言的规则(缺一个括号)——程序拒绝运行。
- 逻辑错误(logic error)能运行但给出错误的结果,因为指令与计划不符。
- 运行时错误(runtime error)在运行时让程序崩溃——比如除以零。
- 溢出错误(overflow error)发生在一个值增长到超过它的存储空间时。
Testing with edge cases (empty input, very large numbers) helps find hidden bugs. · 用边界情况(空输入、很大的数)测试有助于找到隐藏的缺陷。
Bugs often hide at the limits of what a program expects. · 缺陷常常藏在程序所期望的极限处。
To spot a bug in a test, you compare the actual output with the: · 要在测试中发现缺陷,你把实际输出与什么比较:
A difference between actual and expected output points to a bug. · 实际与预期输出之间的差异指向一个缺陷。
How to find them
- Test with many inputs, including unusual ones — a very large number, empty input, or an edge case 边界情况 at the limit.
- Compare the actual output with the expected output 预期输出; a difference points to a bug.
- Add temporary print statements to see internal values, so you find where it first goes wrong.
- Divide and check: confirm one part is correct, then narrow down the rest.
如何找到它们
- 用许多输入测试,包括不寻常的——一个很大的数、空输入,或一个处于极限的边界情况(edge case)。
- 把实际输出与预期输出(expected output)比较;差异指向一个缺陷。
- 加临时的打印语句来查看内部值,这样你能找到它在哪里首次出错。
- 分而查之:确认一部分正确,然后缩小其余部分的范围。
You should re-test the whole program after fixing a bug, since a fix can create a new bug. · 修复一个缺陷后你应该重测整个程序,因为一个修复可能造成新的缺陷。
Careful re-testing is what makes software trustworthy. · 小心的重测让软件值得信任。
Fix, then re-test
- Logic errors are the hardest — nothing obviously "breaks," the answer is just wrong.
- Fixing one bug can create another.
- So after every correction, re-test the whole program.
- Careful, repeated testing turns a rough prototype into software people can trust.
"It ran" is not "it's correct." A logic error runs with no crash and no message — the program looks fine but the answer is wrong. The only way to catch it is to compare the output with what you expected, not just check that it ran.
修复,然后重测
- 逻辑错误最难——没有明显的"崩溃",只是答案错了。
- 修复一个缺陷可能造成另一个。
- 所以在每次更正之后,重测整个程序。
- 小心、反复的测试把一个粗糙的原型变成人们能信任的软件。
"能运行"不等于"正确"。 逻辑错误运行时没有崩溃、没有消息——程序看起来正常但答案是错的。抓住它的唯一办法是把输出与你预期的比较,而不只是检查它是否运行。
A bug is a mistake; fixing bugs is debugging. A syntax error stops the program running, a logic error runs but gives the wrong answer, a runtime error crashes it partway, and an overflow error exceeds storage. Find bugs by testing many inputs (especially edge cases) against the expected output, and always re-test after a fix.
缺陷是一个错误;修复缺陷是调试。语法错误阻止程序运行,逻辑错误能运行但给出错误答案,运行时错误中途让它崩溃,溢出错误超过存储。通过用许多输入(尤其是边界情况)对照预期输出测试来找缺陷,并总在修复后重测。