[Chinese-translation]failfast.chinese.md - add.

This commit is contained in:
matt_jin
2017-12-18 22:10:28 +08:00
parent 7f359a6f37
commit 077af11db3

View File

@ -5,13 +5,13 @@
我们都知道如何检查参数和快速失败对于避免隐藏的错误很重要见下面的反模式代码示例。如果没有请阅读显式编程和防御性编程。在现实中由于对其编码是件恼人的事情比如考虑验证分层的JSON对象它包含像email和日期这样的字段我们倾向于避免做这样的事情 像Joi这样的库和验证器轻而易举的处理这个乏味的任务。
### Wikipedia: Defensive Programming
### 维基百科: 防御性编程
Defensive programming is an approach to improve software and source code, in terms of: General quality reducing the number of software bugs and problems. Making the source code comprehensible the source code should be readable and understandable so it is approved in a code audit. Making the software behave in a predictable manner despite unexpected inputs or user actions.
防御性编程是一种改进软件和源代码的方法, 在以下方面: 一般质量 减少软件 bug 和问题的数量。使源代码可理解 源代码应该是可读的和可理解的, 以便在代码审核中得到批准。尽管会有意外输入或用户操作, 但使软件的行为具有可预知的方式。
### Code example: validating complex JSON input using Joi
### 代码示例: 使用Joi验证复杂的JSON输入
```javascript
var memberSchema = Joi.object().keys({
@ -29,10 +29,10 @@ function addNewMember(newMember)
```
### Anti-pattern: no validation yields nasty bugs
### 反模式: 没有验证会产生令人讨厌的错误
```javascript
//if the discount is positive let's then redirect the user to pring his discount coupons
//假如折扣为正,重定向用户去打印他的折扣优惠劵
function redirectToPrintDiscount(httpResponse, member, discount)
{
if(discount != 0)
@ -40,11 +40,11 @@ function redirectToPrintDiscount(httpResponse, member, discount)
}
redirectToPrintDiscount(httpResponse, someMember);
//forgot to pass the parameter discount, why the heck was the user redirected to the discount screen?
//忘记传递参数discount, 为什么用户被重定向到折扣页面?
```
### Blog Quote: "You should throw these errors immediately"
From the blog: Joyent
### 博客引用: "您应该立即抛出这些错误"
摘自博客: Joyent
> A degenerate case is where someone calls an asynchronous function but doesnt pass a callback. You should throw these errors immediately, since the program is broken and the best chance of debugging it involves getting at least a stack trace and ideally a core file at the point of the error. To do this, we recommend validating the types of all arguments at the start of the function.
> 一个退化情况是有人调用一个异步函数但没有传递一个回调方法。你应该立即抛出这些错误, 因为程序有了错误, 最好的调试它的时机包括获得至少stack trace和理想情况下核心文件里错误的点。为此, 我们建议在函数开始时验证所有参数的类型。