@@ -88,7 +88,7 @@ setTimeout(blastOff, 86400000);
88
88
89
89
** Good:**
90
90
``` javascript
91
- // Declare them as capitalized `const` globals .
91
+ // Declare them as capitalized named constants .
92
92
const MILLISECONDS_IN_A_DAY = 86400000 ;
93
93
94
94
setTimeout (blastOff, MILLISECONDS_IN_A_DAY );
@@ -192,7 +192,7 @@ function createMicrobrewery(name) {
192
192
193
193
** Good:**
194
194
``` javascript
195
- function createMicrobrewery (breweryName = ' Hipster Brew Co.' ) {
195
+ function createMicrobrewery (name = ' Hipster Brew Co.' ) {
196
196
// ...
197
197
}
198
198
@@ -272,13 +272,13 @@ function emailClients(clients) {
272
272
273
273
** Good:**
274
274
``` javascript
275
- function emailClients (clients ) {
275
+ function emailActiveClients (clients ) {
276
276
clients
277
- .filter (isClientActive )
277
+ .filter (isActiveClient )
278
278
.forEach (email);
279
279
}
280
280
281
- function isClientActive (client ) {
281
+ function isActiveClient (client ) {
282
282
const clientRecord = database .lookup (client);
283
283
return clientRecord .isActive ();
284
284
}
@@ -295,7 +295,7 @@ function addToDate(date, month) {
295
295
296
296
const date = new Date ();
297
297
298
- // It's hard to to tell from the function name what is added
298
+ // It's hard to tell from the function name what is added
299
299
addToDate (date, 1 );
300
300
```
301
301
@@ -343,6 +343,14 @@ function parseBetterJSAlternative(code) {
343
343
344
344
** Good:**
345
345
``` javascript
346
+ function parseBetterJSAlternative (code ) {
347
+ const tokens = tokenize (code);
348
+ const ast = lexer (tokens);
349
+ ast .forEach ((node ) => {
350
+ // parse...
351
+ });
352
+ }
353
+
346
354
function tokenize (code ) {
347
355
const REGEXES = [
348
356
// ...
@@ -367,14 +375,6 @@ function lexer(tokens) {
367
375
368
376
return ast;
369
377
}
370
-
371
- function parseBetterJSAlternative (code ) {
372
- const tokens = tokenize (code);
373
- const ast = lexer (tokens);
374
- ast .forEach ((node ) => {
375
- // parse...
376
- });
377
- }
378
378
```
379
379
** [ ⬆ back to top] ( #table-of-contents ) **
380
380
@@ -440,18 +440,20 @@ function showEmployeeList(employees) {
440
440
const expectedSalary = employee .calculateExpectedSalary ();
441
441
const experience = employee .getExperience ();
442
442
443
- let portfolio = employee .getGithubLink ();
444
-
445
- if (employee .type === ' manager' ) {
446
- portfolio = employee .getMBAProjects ();
447
- }
448
-
449
443
const data = {
450
444
expectedSalary,
451
- experience,
452
- portfolio
445
+ experience
453
446
};
454
447
448
+ switch (employee .type ) {
449
+ case ' manager' :
450
+ data .portfolio = employee .getMBAProjects ();
451
+ break ;
452
+ case ' developer' :
453
+ data .githubLink = employee .getGithubLink ();
454
+ break ;
455
+ }
456
+
455
457
render (data);
456
458
});
457
459
}
@@ -600,7 +602,7 @@ holding onto a reference of the shopping cart will be affected by any changes.
600
602
601
603
Two caveats to mention to this approach:
602
604
1 . There might be cases where you actually want to modify the input object,
603
- but when you adopt this programming practice you will find that those case
605
+ but when you adopt this programming practice you will find that those cases
604
606
are pretty rare. Most things can be refactored to have no side effects!
605
607
606
608
2 . Cloning big objects can be very expensive in terms of performance. Luckily,
@@ -619,7 +621,7 @@ const addItemToCart = (cart, item) => {
619
621
** Good:**
620
622
``` javascript
621
623
const addItemToCart = (cart , item ) => {
622
- return [... cart, { item, date : Date .now () }];
624
+ return [... cart, { item, date: Date .now () }];
623
625
};
624
626
```
625
627
@@ -657,7 +659,7 @@ class SuperArray extends Array {
657
659
658
660
### Favor functional programming over imperative programming
659
661
JavaScript isn't a functional language in the way that Haskell is, but it has
660
- a functional flavor to it. Functional languages are cleaner and easier to test.
662
+ a functional flavor to it. Functional languages can be cleaner and easier to test.
661
663
Favor this style of programming when you can.
662
664
663
665
** Bad:**
@@ -703,11 +705,9 @@ const programmerOutput = [
703
705
}
704
706
];
705
707
706
- const INITIAL_VALUE = 0 ;
707
-
708
708
const totalOutput = programmerOutput
709
- .map (( programmer ) => programmer .linesOfCode )
710
- .reduce ((acc , linesOfCode ) => acc + linesOfCode, INITIAL_VALUE );
709
+ .map (output => output .linesOfCode )
710
+ .reduce ((totalLines , lines ) => totalLines + lines );
711
711
```
712
712
** [ ⬆ back to top] ( #table-of-contents ) **
713
713
@@ -839,7 +839,7 @@ function travelToTexas(vehicle) {
839
839
** [ ⬆ back to top] ( #table-of-contents ) **
840
840
841
841
### Avoid type-checking (part 2)
842
- If you are working with basic primitive values like strings, integers, and arrays ,
842
+ If you are working with basic primitive values like strings and integers ,
843
843
and you can't use polymorphism but you still feel the need to type-check,
844
844
you should consider using TypeScript. It is an excellent alternative to normal
845
845
JavaScript, as it provides you with static typing on top of standard JavaScript
@@ -1110,10 +1110,10 @@ and you can chain further class methods onto it.
1110
1110
** Bad:**
1111
1111
``` javascript
1112
1112
class Car {
1113
- constructor () {
1114
- this .make = ' Honda ' ;
1115
- this .model = ' Accord ' ;
1116
- this .color = ' white ' ;
1113
+ constructor (make , model , color ) {
1114
+ this .make = make ;
1115
+ this .model = model ;
1116
+ this .color = color ;
1117
1117
}
1118
1118
1119
1119
setMake (make ) {
@@ -1133,20 +1133,18 @@ class Car {
1133
1133
}
1134
1134
}
1135
1135
1136
- const car = new Car ();
1136
+ const car = new Car (' Ford ' , ' F-150 ' , ' red ' );
1137
1137
car .setColor (' pink' );
1138
- car .setMake (' Ford' );
1139
- car .setModel (' F-150' );
1140
1138
car .save ();
1141
1139
```
1142
1140
1143
1141
** Good:**
1144
1142
``` javascript
1145
1143
class Car {
1146
- constructor () {
1147
- this .make = ' Honda ' ;
1148
- this .model = ' Accord ' ;
1149
- this .color = ' white ' ;
1144
+ constructor (make , model , color ) {
1145
+ this .make = make ;
1146
+ this .model = model ;
1147
+ this .color = color ;
1150
1148
}
1151
1149
1152
1150
setMake (make ) {
@@ -1174,10 +1172,8 @@ class Car {
1174
1172
}
1175
1173
}
1176
1174
1177
- const car = new Car ()
1175
+ const car = new Car (' Ford ' , ' F-150 ' , ' red ' )
1178
1176
.setColor (' pink' )
1179
- .setMake (' Ford' )
1180
- .setModel (' F-150' )
1181
1177
.save ();
1182
1178
```
1183
1179
** [ ⬆ back to top] ( #table-of-contents ) **
@@ -1687,7 +1683,7 @@ you achieve very high confidence and developer peace of mind. This means that
1687
1683
in addition to having a great testing framework, you also need to use a
1688
1684
[ good coverage tool] ( http://gotwarlost.github.io/istanbul/ ) .
1689
1685
1690
- There's no excuse to not write tests. There's [ plenty of good JS test frameworks] ( http://jstherightway.org/#testing-tools ) , so find one that your team prefers.
1686
+ There's no excuse to not write tests. There are [ plenty of good JS test frameworks] ( http://jstherightway.org/#testing-tools ) , so find one that your team prefers.
1691
1687
When you find one that works for your team, then aim to always write tests
1692
1688
for every new feature/module you introduce. If your preferred method is
1693
1689
Test Driven Development (TDD), that is great, but the main point is to just
@@ -1941,8 +1937,8 @@ class Alpaca {}
1941
1937
const DAYS_IN_WEEK = 7 ;
1942
1938
const DAYS_IN_MONTH = 30 ;
1943
1939
1944
- const songs = [' Back In Black' , ' Stairway to Heaven' , ' Hey Jude' ];
1945
- const artists = [' ACDC' , ' Led Zeppelin' , ' The Beatles' ];
1940
+ const SONGS = [' Back In Black' , ' Stairway to Heaven' , ' Hey Jude' ];
1941
+ const ARTISTS = [' ACDC' , ' Led Zeppelin' , ' The Beatles' ];
1946
1942
1947
1943
function eraseDatabase () {}
1948
1944
function restoreDatabase () {}
@@ -2170,9 +2166,15 @@ This is also available in other languages:
2170
2166
- [ beginor/clean-code-javascript] ( https://github.com/beginor/clean-code-javascript )
2171
2167
- ![ de] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Germany.png ) ** German** : [ marcbruederlin/clean-code-javascript] ( https://github.com/marcbruederlin/clean-code-javascript )
2172
2168
- ![ kr] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/South-Korea.png ) ** Korean** : [ qkraudghgh/clean-code-javascript-ko] ( https://github.com/qkraudghgh/clean-code-javascript-ko )
2169
+ - ![ pl] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Poland.png ) ** Polish** : [ greg-dev/clean-code-javascript-pl] ( https://github.com/greg-dev/clean-code-javascript-pl )
2173
2170
- ![ ru] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png ) ** Russian** :
2174
2171
- [ BoryaMogila/clean-code-javascript-ru/] ( https://github.com/BoryaMogila/clean-code-javascript-ru/ )
2175
2172
- [ maksugr/clean-code-javascript] ( https://github.com/maksugr/clean-code-javascript )
2176
2173
- ![ vi] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Vietnam.png ) ** Vietnamese** : [ hienvd/clean-code-javascript/] ( https://github.com/hienvd/clean-code-javascript/ )
2174
+ - ![ ja] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Japan.png ) ** Japanese** : [ mitsuruog/clean-code-javascript/] ( https://github.com/mitsuruog/clean-code-javascript/ )
2175
+ - ![ id] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Indonesia.png ) ** Indonesia** :
2176
+ [ andirkh/clean-code-javascript/] ( https://github.com/andirkh/clean-code-javascript/ )
2177
+ - ![ it] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Italy.png ) ** Italian** :
2178
+ [ frappacchio/clean-code-javascript/] ( https://github.com/frappacchio/clean-code-javascript/ )
2177
2179
2178
2180
** [ ⬆ back to top] ( #table-of-contents ) **
0 commit comments