From 4957071107a26a4f950c6d180188e5d000d67836 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Wed, 12 Aug 2015 11:04:27 +0200 Subject: [PATCH 1/7] Allow implicit return + one multiline expression --- README.md | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 0ec7fec1..48bb3b2f 100644 --- a/README.md +++ b/README.md @@ -602,7 +602,9 @@ }); ``` - - [8.2](#8.2) If the function body fits on one line and there is only a single argument, feel free to omit the braces and parentheses, and use the implicit return. Otherwise, add the parentheses, braces, and use a `return` statement. + - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise use a `return` statement. + + In case the expression spans over multiple lines, wrap it in parentheses for better readability. > Why? Syntactic sugar. It reads well when multiple functions are chained together. @@ -610,12 +612,25 @@ ```javascript // good - [1, 2, 3].map(x => x * x); + [1, 2, 3].map(number => `A string containing the ${number}.`); // good - [1, 2, 3].reduce((total, n) => { - return total + n; - }, 0); + [1, 2, 3].map(number => ( + `As time went by, the string containing the ${number} became much ` + + 'longer. So we needed to break it over multiple lines.' + )); + + // bad + [1, 2, 3].map(number => { + let nextNumber = number + 1; + `A string containing the ${nextNumber}.`; + }); + + // good + [1, 2, 3].map(number => { + let nextNumber = number + 1; + return `A string containing the ${nextNumber}.`; + }); ``` **[⬆ back to top](#table-of-contents)** From 5634c4c88e5293e83cccf5dd9055666e9400c1de Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Wed, 12 Aug 2015 11:04:50 +0200 Subject: [PATCH 2/7] Update existing example --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 48bb3b2f..9b362d97 100644 --- a/README.md +++ b/README.md @@ -597,9 +597,7 @@ }); // good - [1, 2, 3].map((x) => { - return x * x; - }); + [1, 2, 3].map(x => x * x); ``` - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise use a `return` statement. From 5b8b56ece733543787c07d60c5f93efc5bbb2b6f Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Wed, 12 Aug 2015 11:09:14 +0200 Subject: [PATCH 3/7] Upgrade a note about parens to another rule --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 9b362d97..fb9020ca 100644 --- a/README.md +++ b/README.md @@ -631,6 +631,18 @@ }); ``` + - [8.3](#8.3) If your function only takes a single argument, feel free to omit the parentheses. + + > Why? Less visual clutter. + + ```js + // good + [1, 2, 3].map(x => x * x); + + // good + [1, 2, 3].reduce((y, x) => x + y); + ``` + **[⬆ back to top](#table-of-contents)** From 3aabe2c6934a2fef474cde5f0e48172b45979fc5 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Thu, 13 Aug 2015 08:43:33 +0200 Subject: [PATCH 4/7] Upgrade half of 8.2 to another rule --- README.md | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index fb9020ca..8e0863ca 100644 --- a/README.md +++ b/README.md @@ -602,8 +602,6 @@ - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise use a `return` statement. - In case the expression spans over multiple lines, wrap it in parentheses for better readability. - > Why? Syntactic sugar. It reads well when multiple functions are chained together. > Why not? If you plan on returning an object. @@ -612,12 +610,6 @@ // good [1, 2, 3].map(number => `A string containing the ${number}.`); - // good - [1, 2, 3].map(number => ( - `As time went by, the string containing the ${number} became much ` + - 'longer. So we needed to break it over multiple lines.' - )); - // bad [1, 2, 3].map(number => { let nextNumber = number + 1; @@ -631,7 +623,26 @@ }); ``` - - [8.3](#8.3) If your function only takes a single argument, feel free to omit the parentheses. + - [8.3](#8.3) In case the expression spans over multiple lines, wrap it in parentheses for better readability. + + > Why? It shows clearly where the function starts and ends. + + ```js + // bad + [1, 2, 3].map(number => 'As time went by, the string containing the ' + + `${number} became much longer. So we needed to break it over multiple ` + + 'lines.' + ); + + // good + [1, 2, 3].map(number => ( + `As time went by, the string containing the ${number} became much ` + + 'longer. So we needed to break it over multiple lines.' + )); + ``` + + + - [8.4](#8.4) If your function only takes a single argument, feel free to omit the parentheses. > Why? Less visual clutter. From 60360bdedd1c282502af0264130cfde9d5666c17 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Thu, 13 Aug 2015 08:43:59 +0200 Subject: [PATCH 5/7] Match 8.4 indentation to other rules --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e0863ca..3b1bba85 100644 --- a/README.md +++ b/README.md @@ -644,7 +644,7 @@ - [8.4](#8.4) If your function only takes a single argument, feel free to omit the parentheses. - > Why? Less visual clutter. + > Why? Less visual clutter. ```js // good From 417f9896d0ae1088aafbc5c223ca3596cb9f4811 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Thu, 13 Aug 2015 08:47:48 +0200 Subject: [PATCH 6/7] Bring back explicit return in 8.1 example --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3b1bba85..724a4dea 100644 --- a/README.md +++ b/README.md @@ -593,11 +593,15 @@ ```javascript // bad [1, 2, 3].map(function (x) { - return x * x; + const y = x + 1; + return x * y; }); // good - [1, 2, 3].map(x => x * x); + [1, 2, 3].map((x) => { + const y = x + 1; + return x * y; + }); ``` - [8.2](#8.2) If the function body consists of a single expression, feel free to omit the braces and use the implicit return. Otherwise use a `return` statement. From bfbb074ac95e3b277cda3b6a168d6f2c73d845b5 Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Thu, 13 Aug 2015 08:51:04 +0200 Subject: [PATCH 7/7] Oops, keep to 13.1! --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 724a4dea..75aff342 100644 --- a/README.md +++ b/README.md @@ -616,13 +616,13 @@ // bad [1, 2, 3].map(number => { - let nextNumber = number + 1; + const nextNumber = number + 1; `A string containing the ${nextNumber}.`; }); // good [1, 2, 3].map(number => { - let nextNumber = number + 1; + const nextNumber = number + 1; return `A string containing the ${nextNumber}.`; }); ```