|
1 | 1 | # clean-code-javascript
|
2 |
| -Software engineering principles, from Robert C. Martin's wonderful book |
3 |
| -*Clean Code*, adapted for JavaScript. This is not a style guide, it's something |
4 |
| -much more. It's a guide to producing readable, reusable, and refactorable |
5 |
| -JavaScript software. Enjoy! |
| 2 | +Software engineering principles, from Robert C. Martin's book |
| 3 | +[*Clean Code*](https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882), |
| 4 | +adapted for JavaScript. This is not a style guide. It's a guide to producing |
| 5 | +readable, reusable, and refactorable software in JavaScript. Enjoy! |
6 | 6 |
|
7 | 7 | ## Table of Contents
|
8 | 8 | 1. [Variables](#variables)
|
@@ -323,6 +323,62 @@ class SuperArray extends Array {
|
323 | 323 | ```
|
324 | 324 | **[⬆ back to top](#table-of-contents)**
|
325 | 325 |
|
| 326 | +### Favor functional programming over imperative programming |
| 327 | +If Haskell were an IPA then JavaScript would be an O'Douls. That is to say, |
| 328 | +JavaScript isn't a functional language in the way that Haskell is, but it has |
| 329 | +a functional flavor to it. Functional languages are cleaner and easier to test. |
| 330 | +Favor this style of programming when you can. |
| 331 | + |
| 332 | +**Bad:** |
| 333 | +```javascript |
| 334 | +const programmerOutput = [ |
| 335 | + { |
| 336 | + name: 'Uncle Bobby', |
| 337 | + linesOfCode: 500 |
| 338 | + }, { |
| 339 | + name: 'Suzie Q', |
| 340 | + linesOfCode: 1500 |
| 341 | + }, { |
| 342 | + name: 'Jimmy Gosling', |
| 343 | + linesOfCode: 150 |
| 344 | + }, { |
| 345 | + name: 'Gracie Hopper', |
| 346 | + linesOfCode: 1000 |
| 347 | + } |
| 348 | +]; |
| 349 | + |
| 350 | +var totalOutput = 0; |
| 351 | + |
| 352 | +for (var i = 0; i < programmerOutput.length; i++) { |
| 353 | + totalOutput += programmerOutput[i].linesOfCode; |
| 354 | +} |
| 355 | +``` |
| 356 | + |
| 357 | +**Good**: |
| 358 | +```javascript |
| 359 | +const programmerOutput = [ |
| 360 | + { |
| 361 | + name: 'Uncle Bobby', |
| 362 | + linesOfCode: 500 |
| 363 | + }, { |
| 364 | + name: 'Suzie Q', |
| 365 | + linesOfCode: 1500 |
| 366 | + }, { |
| 367 | + name: 'Jimmy Gosling', |
| 368 | + linesOfCode: 150 |
| 369 | + }, { |
| 370 | + name: 'Gracie Hopper', |
| 371 | + linesOfCode: 1000 |
| 372 | + } |
| 373 | +]; |
| 374 | + |
| 375 | +var totalOutput = programmerOutput |
| 376 | + .map((programmer) => programmer.linesOfCode) |
| 377 | + .reduce((acc, linesOfCode) => acc + linesOfCode, 0); |
| 378 | +``` |
| 379 | +**[⬆ back to top](#table-of-contents)** |
| 380 | + |
| 381 | + |
326 | 382 | ## **Classes**
|
327 | 383 | ### Prefer ES6 classes over ES5 plain functions
|
328 | 384 | It's very difficult to get readable class inheritance, construction, and method
|
|
0 commit comments