@@ -88,7 +88,7 @@ setTimeout(blastOff, 86400000);
8888
8989** Good:**
9090``` javascript
91- // Declare them as capitalized `const` globals .
91+ // Declare them as capitalized named constants .
9292const MILLISECONDS_IN_A_DAY = 86400000 ;
9393
9494setTimeout (blastOff, MILLISECONDS_IN_A_DAY );
@@ -192,7 +192,7 @@ function createMicrobrewery(name) {
192192
193193** Good:**
194194``` javascript
195- function createMicrobrewery (breweryName = ' Hipster Brew Co.' ) {
195+ function createMicrobrewery (name = ' Hipster Brew Co.' ) {
196196 // ...
197197}
198198
@@ -272,13 +272,13 @@ function emailClients(clients) {
272272
273273** Good:**
274274``` javascript
275- function emailClients (clients ) {
275+ function emailActiveClients (clients ) {
276276 clients
277- .filter (isClientActive )
277+ .filter (isActiveClient )
278278 .forEach (email);
279279}
280280
281- function isClientActive (client ) {
281+ function isActiveClient (client ) {
282282 const clientRecord = database .lookup (client);
283283 return clientRecord .isActive ();
284284}
@@ -295,7 +295,7 @@ function addToDate(date, month) {
295295
296296const date = new Date ();
297297
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
299299addToDate (date, 1 );
300300```
301301
@@ -343,6 +343,14 @@ function parseBetterJSAlternative(code) {
343343
344344** Good:**
345345``` javascript
346+ function parseBetterJSAlternative (code ) {
347+ const tokens = tokenize (code);
348+ const ast = lexer (tokens);
349+ ast .forEach ((node ) => {
350+ // parse...
351+ });
352+ }
353+
346354function tokenize (code ) {
347355 const REGEXES = [
348356 // ...
@@ -367,14 +375,6 @@ function lexer(tokens) {
367375
368376 return ast;
369377}
370-
371- function parseBetterJSAlternative (code ) {
372- const tokens = tokenize (code);
373- const ast = lexer (tokens);
374- ast .forEach ((node ) => {
375- // parse...
376- });
377- }
378378```
379379** [ ⬆ back to top] ( #table-of-contents ) **
380380
@@ -440,18 +440,20 @@ function showEmployeeList(employees) {
440440 const expectedSalary = employee .calculateExpectedSalary ();
441441 const experience = employee .getExperience ();
442442
443- let portfolio = employee .getGithubLink ();
444-
445- if (employee .type === ' manager' ) {
446- portfolio = employee .getMBAProjects ();
447- }
448-
449443 const data = {
450444 expectedSalary,
451- experience,
452- portfolio
445+ experience
453446 };
454447
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+
455457 render (data);
456458 });
457459}
@@ -619,7 +621,7 @@ const addItemToCart = (cart, item) => {
619621** Good:**
620622``` javascript
621623const addItemToCart = (cart , item ) => {
622- return [... cart, { item, date : Date .now () }];
624+ return [... cart, { item, date: Date .now () }];
623625};
624626```
625627
@@ -657,7 +659,7 @@ class SuperArray extends Array {
657659
658660### Favor functional programming over imperative programming
659661JavaScript 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.
661663Favor this style of programming when you can.
662664
663665** Bad:**
@@ -703,11 +705,9 @@ const programmerOutput = [
703705 }
704706];
705707
706- const INITIAL_VALUE = 0 ;
707-
708708const 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 );
711711```
712712** [ ⬆ back to top] ( #table-of-contents ) **
713713
@@ -839,7 +839,7 @@ function travelToTexas(vehicle) {
839839** [ ⬆ back to top] ( #table-of-contents ) **
840840
841841### 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 ,
843843and you can't use polymorphism but you still feel the need to type-check,
844844you should consider using TypeScript. It is an excellent alternative to normal
845845JavaScript, 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.
11101110** Bad:**
11111111``` javascript
11121112class 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 ;
11171117 }
11181118
11191119 setMake (make ) {
@@ -1133,20 +1133,18 @@ class Car {
11331133 }
11341134}
11351135
1136- const car = new Car ();
1136+ const car = new Car (' Ford ' , ' F-150 ' , ' red ' );
11371137car .setColor (' pink' );
1138- car .setMake (' Ford' );
1139- car .setModel (' F-150' );
11401138car .save ();
11411139```
11421140
11431141** Good:**
11441142``` javascript
11451143class 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 ;
11501148 }
11511149
11521150 setMake (make ) {
@@ -1174,10 +1172,8 @@ class Car {
11741172 }
11751173}
11761174
1177- const car = new Car ()
1175+ const car = new Car (' Ford ' , ' F-150 ' , ' red ' )
11781176 .setColor (' pink' )
1179- .setMake (' Ford' )
1180- .setModel (' F-150' )
11811177 .save ();
11821178```
11831179** [ ⬆ back to top] ( #table-of-contents ) **
@@ -1687,7 +1683,7 @@ you achieve very high confidence and developer peace of mind. This means that
16871683in addition to having a great testing framework, you also need to use a
16881684[ good coverage tool] ( http://gotwarlost.github.io/istanbul/ ) .
16891685
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.
16911687When you find one that works for your team, then aim to always write tests
16921688for every new feature/module you introduce. If your preferred method is
16931689Test Driven Development (TDD), that is great, but the main point is to just
@@ -1941,8 +1937,8 @@ class Alpaca {}
19411937const DAYS_IN_WEEK = 7 ;
19421938const DAYS_IN_MONTH = 30 ;
19431939
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' ];
19461942
19471943function eraseDatabase () {}
19481944function restoreDatabase () {}
@@ -2170,10 +2166,13 @@ This is also available in other languages:
21702166 - [ beginor/clean-code-javascript] ( https://github.com/beginor/clean-code-javascript )
21712167 - ![ 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 )
21722168 - ![ 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 )
21732170 - ![ ru] ( https://raw.githubusercontent.com/gosquared/flags/master/flags/flags/shiny/24/Russia.png ) ** Russian** :
21742171 - [ BoryaMogila/clean-code-javascript-ru/] ( https://github.com/BoryaMogila/clean-code-javascript-ru/ )
21752172 - [ maksugr/clean-code-javascript] ( https://github.com/maksugr/clean-code-javascript )
21762173 - ![ 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/ )
21772174 - ![ 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/ )
21782177
21792178** [ ⬆ back to top] ( #table-of-contents ) **
0 commit comments