From 3f7f7feb354efbb1a0294d3c02d6e8ded29c5fcf Mon Sep 17 00:00:00 2001 From: Yuki Ota Date: Thu, 22 Oct 2020 21:28:19 +0900 Subject: [PATCH] translate 2.11 --- README.japanese.md | 8 ++--- sections/errorhandling/failfast.japanese.md | 34 ++++++++++----------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/README.japanese.md b/README.japanese.md index 24c3f757..7f925f14 100644 --- a/README.japanese.md +++ b/README.japanese.md @@ -216,13 +216,13 @@

-## ![✔] 2.11 Fail fast, validate arguments using a dedicated library +## ![✔] 2.11 専用のライブラリを利用して引数の検証を高速に行う -**TL;DR:** Assert API input to avoid nasty bugs that are much harder to track later. The validation code is usually tedious unless you are using a very cool helper library like [ajv](https://www.npmjs.com/package/ajv) and [Joi](https://www.npmjs.com/package/joi) +**TL;DR:** API の入力をアサートすることで、後から追跡するのが非常に難しい厄介なバグを避けることができます。[ajv](https://www.npmjs.com/package/ajv) や [Joi](https://www.npmjs.com/package/joi) のような非常にクールなヘルパーライブラリを利用しない限り、バリデーションコードを書くことは一般的に退屈な作業です。 -**Otherwise:** Consider this – your function expects a numeric argument “Discount” which the caller forgets to pass, later on, your code checks if Discount!=0 (amount of allowed discount is greater than zero), then it will allow the user to enjoy a discount. OMG, what a nasty bug. Can you see it? +**さもないと:** 考えてみて下さい ー 関数は数値の引数「Discount」を受け取ることを期待していますが、呼び出し元が値を渡すのを忘れてしまいました。その後、コードが Discount!=0 (許容されたディスカウントの量が 0 よりも大きいことを想定) であるということをチェックし、そのチェックをクリアした場合にユーザーがディスカウントを受けられるようにしました。オーマイガー、なんて厄介なバグなんでしょう。わかりますか?(訳注:「さらに読む」に具体的なコード例が載っています) -🔗 [**Read More: failing fast**](/sections/errorhandling/failfast.md) +🔗 [**さらに読む: 専用のライブラリを利用して引数の検証を高速に行う**](/sections/errorhandling/failfast.japanese.md)


diff --git a/sections/errorhandling/failfast.japanese.md b/sections/errorhandling/failfast.japanese.md index 805d7ac9..65963898 100644 --- a/sections/errorhandling/failfast.japanese.md +++ b/sections/errorhandling/failfast.japanese.md @@ -1,14 +1,14 @@ -# Fail fast, validate arguments using a dedicated library +# 専用のライブラリを利用して引数の検証を高速に行う -### One Paragraph Explainer +### 一段落説明 -We all know how checking arguments and failing fast is important to avoid hidden bugs (see anti-pattern code example below). If not, read about explicit programming and defensive programming. In reality, we tend to avoid it due to the annoyance of coding it (e.g. think of validating hierarchical JSON object with fields like email and dates) – libraries like Joi and Validator turn this tedious task into a breeze. +隠れたバグを避けるためには、引数をチェックすること、そして高速に失敗することが重要であると誰もが知っています(下記のアンチパターンコード例を参照)。もし知らないのであれば、明示的プログラミングと防御的プログラミングについて読んでみてください。実際には、コーディングをするのが面倒なので避けがちですが(例えば、メールアドレスや日時のようなフィールドを持つ階層的な JSON オブジェクトの検証をすることを考えてみてください)、Joi や Validator のようなライブラリはこの面倒なタスクを簡単にしてくれます。 -### 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. +防御的プログラミング(Defensive programming)は、ソフトウェアのバグや問題の数を減少させるという一般的な品質の観点において、ソフトウェアやソースコードを改善するためのアプローチです。ソースコードを理解しやすいものにすること ー コード監査で承認されるように、ソースコードは可読性が高く、わかりやすいものであるべきです。想定外の入力やユーザーアクションに対しても、ソフトウェアに予測可能な挙動をさせるべきです。 -### Code example: validating complex JSON input using ‘Joi’ +### コード例: 「Joi」を利用して複雑な JSON 形式の入力を検証する ```javascript var memberSchema = Joi.object().keys({ @@ -18,21 +18,21 @@ var memberSchema = Joi.object().keys({ }); function addNewMember(newMember) { - // assertions come first - Joi.assert(newMember, memberSchema); //throws if validation fails - // other logic here + // アサーションがまず最初に来る + Joi.assert(newMember, memberSchema); // もし検証が失敗したら例外を投げます + // その他のロジックがここに来ます } ``` -### Anti-pattern: no validation yields nasty bugs +### アンチパターン: 検証をしないと厄介なバグが発生する
Javascript ```javascript -// if the discount is positive let's then redirect the user to print his discount coupons +// もし discount が正の値なら、ユーザーを割引クーポンを発行するためにユーザーをリダイレクトさせましょう function redirectToPrintDiscount(httpResponse, member, discount) { if (discount != 0) { httpResponse.redirect(`/discountPrintView/${member.id}`); @@ -40,7 +40,7 @@ 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 パラメータを渡すのを忘れてしまいました。一体なぜユーザーは割引クーポン発行画面へリダイレクトされたのでしょうか? ```
@@ -48,7 +48,7 @@ redirectToPrintDiscount(httpResponse, someMember); Typescript ```typescript -// if the discount is positive let's then redirect the user to print his discount coupons +// もし discount が正の値なら、ユーザーを割引クーポンを発行するためにユーザーをリダイレクトさせましょう function redirectToPrintDiscount(httpResponse: Response, member: Member, discount: number) { if (discount != 0) { httpResponse.redirect(`/discountPrintView/${member.id}`); @@ -56,12 +56,12 @@ function redirectToPrintDiscount(httpResponse: Response, member: Member, discoun } redirectToPrintDiscount(httpResponse, someMember, -12); -// We passed a negative parameter discount, why the heck was the user redirected to the discount screen? +// discount パラメータとして負の値を渡しました。一体なぜユーザーは割引クーポン発行画面へリダイレクトされたのでしょうか? ``` -### Blog Quote: "You should throw these errors immediately" +### ブログ引用: "You should throw these errors immediately"(エラーは直ちに投げるべきです) - From the blog: Joyent +ブログ Joyentより - > A degenerate case is where someone calls an asynchronous function but doesn’t 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. +> 悪化したケースとして、非同期関数を呼び出したもののコールバックを渡さなかった場合があります。プログラムは壊れていますし、デバッグに最適なのは少なくともスタックトレースを取得、理想的にはエラーが発生した地点のコアファイルを取得することなので、このようなエラーは直ちに投げるべきです。これを行うために、関数の開始時にすべての引数の型を検証することをおすすめします。