From dfb9544da58927596d6c2aea294fb98ca55a7590 Mon Sep 17 00:00:00 2001 From: Anton Paras Date: Thu, 5 Jan 2017 20:11:35 -0800 Subject: [PATCH 1/3] Rephrase default value advice - The original document asserted that "Short-circuiting is cleaner than conditionals" - ...only to override this assertion later with "Use default arguments instead of short circuiting." - This is a poor design-choice for an advisory document. - If you are going to say x > y, - then later say z > x, - you should just say z > x > y upfront. - Otherwise, many people might not see the z > x later on, - because many people will probably use this as a referential document. They probably won't read the entire work. --- README.md | 38 +++++++++++++++----------------------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index d0769786..a43ce340 100644 --- a/README.md +++ b/README.md @@ -188,25 +188,36 @@ function paintCar(car) { ``` **[⬆ back to top](#table-of-contents)** -### Short-circuiting is cleaner than conditionals +### Use default arguments instead of short circuiting or conditionals **Bad:** ```javascript function createMicrobrewery(name) { - var breweryName; + let breweryName; if (name) { breweryName = name; } else { breweryName = 'Hipster Brew Co.'; } + ... } ``` -**Good**: +**Better:** ```javascript function createMicrobrewery(name) { - var breweryName = name || 'Hipster Brew Co.' + const breweryName = name || 'Hipster Brew Co.'; + ... +} + +``` + +**Best**: +```javascript +function createMicrobrewery(breweryName = 'Hipster Brew Co.') { + ... } + ``` **[⬆ back to top](#table-of-contents)** @@ -450,25 +461,6 @@ function showList(employees) { ``` **[⬆ back to top](#table-of-contents)** -### Use default arguments instead of short circuiting -**Bad:** -```javascript -function writeForumComment(subject, body) { - subject = subject || 'No Subject'; - body = body || 'No text'; -} - -``` - -**Good**: -```javascript -function writeForumComment(subject = 'No subject', body = 'No text') { - ... -} - -``` -**[⬆ back to top](#table-of-contents)** - ### Set default objects with Object.assign **Bad:** From 8be1793b3335f71956555e154eba0735a2ddce4a Mon Sep 17 00:00:00 2001 From: Anton Paras Date: Fri, 6 Jan 2017 18:37:54 -0800 Subject: [PATCH 2/3] Change Bad/Better/Best to Bad/Bad/Good - This commit concerns the document's comparison of 3 approaches to default values. - Conditionals - Short-Circuiting - Default Arguments - In my original pull request, I ordered these from bad to best: - Conditionals (Bad) - Short-Circuiting (Better) - Default Arguments (Best) - In this commit, I am rephrasing the **Bad/Better/Best** ordering scheme to **Bad/Bad/Good** because: - The rest of the document uses the **Bad/Good** format. - We should use *consistent language* throughout the document. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a43ce340..bbdcde2d 100644 --- a/README.md +++ b/README.md @@ -203,7 +203,7 @@ function createMicrobrewery(name) { } ``` -**Better:** +**Bad:** ```javascript function createMicrobrewery(name) { const breweryName = name || 'Hipster Brew Co.'; @@ -212,7 +212,7 @@ function createMicrobrewery(name) { ``` -**Best**: +**Good**: ```javascript function createMicrobrewery(breweryName = 'Hipster Brew Co.') { ... From eaa3477dbfda8b555647d484a35dec7aebb8944d Mon Sep 17 00:00:00 2001 From: Anton Paras Date: Tue, 10 Jan 2017 08:53:09 -0800 Subject: [PATCH 3/3] Lower "bad example" count -The section, "Use default arguments instead of short circuiting or conditionals" originally had 2 "bad examples" and 1 "good example." - This commit lowers the "bad examples" count from 2 -> 1. - The 1st "bad example" demonstrated the "conditionals" approach to default values. - The 2nd "bad example" demonstrated the "short-circuit" approach to default values. - I decided to only show the "short-circuit" approach as the 1 bad example, since that's what most people would use traditionally in ES5. - Few people actually use the "conditionals" approach, so I felt that warning against the "conditionals" approach was unnecessary. --- README.md | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/README.md b/README.md index 99bbc403..f6257d1e 100644 --- a/README.md +++ b/README.md @@ -173,19 +173,6 @@ function paintCar(car) { ### Use default arguments instead of short circuiting or conditionals -**Bad:** -```javascript -function createMicrobrewery(name) { - let breweryName; - if (name) { - breweryName = name; - } else { - breweryName = 'Hipster Brew Co.'; - } - ... -} -``` - **Bad:** ```javascript function createMicrobrewery(name) {