File tree
Expand file treeCollapse file tree1 file changed
+7
-28
lines changed Expand file treeCollapse file tree1 file changed
+7
-28
lines changed Original file line number | Diff line number | Diff line change |
---|
@@ -174,25 +174,23 @@ function paintCar(car) {
|
174 | 174 | ```
|
175 | 175 | **[⬆ back to top](#table-of-contents)**
|
176 | 176 |
|
177 |
| -### Short-circuiting is cleaner than conditionals |
| 177 | +### Use default arguments instead of short circuiting or conditionals |
178 | 178 |
|
179 | 179 | **Bad:**
|
180 | 180 | ```javascript
|
181 | 181 | function createMicrobrewery(name) {
|
182 |
| -let breweryName; |
183 |
| -if (name) { |
184 |
| -breweryName = name; |
185 |
| -} else { |
186 |
| -breweryName = 'Hipster Brew Co.'; |
187 |
| -} |
| 182 | +const breweryName = name || 'Hipster Brew Co.'; |
| 183 | +... |
188 | 184 | }
|
| 185 | + |
189 | 186 | ```
|
190 | 187 |
|
191 | 188 | **Good**:
|
192 | 189 | ```javascript
|
193 |
| -function createMicrobrewery(name) { |
194 |
| -const breweryName = name || 'Hipster Brew Co.' |
| 190 | +function createMicrobrewery(breweryName = 'Hipster Brew Co.') { |
| 191 | +... |
195 | 192 | }
|
| 193 | + |
196 | 194 | ```
|
197 | 195 | **[⬆ back to top](#table-of-contents)**
|
198 | 196 |
|
@@ -431,25 +429,6 @@ function showList(employees) {
|
431 | 429 | ```
|
432 | 430 | **[⬆ back to top](#table-of-contents)**
|
433 | 431 |
|
434 |
| -### Use default arguments instead of short circuiting |
435 |
| -**Bad:** |
436 |
| -```javascript |
437 |
| -function writeForumComment(subject, body) { |
438 |
| -subject = subject || 'No Subject'; |
439 |
| -body = body || 'No text'; |
440 |
| -} |
441 |
| - |
442 |
| -``` |
443 |
| - |
444 |
| -**Good**: |
445 |
| -```javascript |
446 |
| -function writeForumComment(subject = 'No subject', body = 'No text') { |
447 |
| -// ... |
448 |
| -} |
449 |
| - |
450 |
| -``` |
451 |
| -**[⬆ back to top](#table-of-contents)** |
452 |
| - |
453 | 432 | ### Set default objects with Object.assign
|
454 | 433 |
|
455 | 434 | **Bad:**
|
|
You can’t perform that action at this time.
0 commit comments