@@ -72,7 +72,10 @@ getUser();
7272We will read more code than we will ever write. It's important that the code we
7373do write is readable and searchable. By * not* naming variables that end up
7474being meaningful for understanding our program, we hurt our readers.
75- Make your names searchable.
75+ Make your names searchable. Tools like
76+ [ buddy.js] ( https://github.com/danielstjules/buddy.js ) and
77+ [ ESLint] ( https://github.com/eslint/eslint/blob/660e0918933e6e7fede26bc675a0763a6b357c94/docs/rules/no-magic-numbers.md )
78+ can help identify unnamed constants.
7679
7780** Bad:**
7881``` javascript
@@ -226,7 +229,7 @@ const menuConfig = {
226229 cancellable: true
227230}
228231
229- function createMenu (menuConfig ) {
232+ function createMenu (config ) {
230233 // ...
231234}
232235
@@ -367,7 +370,7 @@ for it and it's quite possibly the worst sin you can commit as a professional
367370developer. Duplicate code means there's more than one place to alter something
368371if you need to change some logic. JavaScript is untyped, so it makes having
369372generic functions quite easy. Take advantage of that! Tools like
370- [ jsinpect ] ( https://github.com/danielstjules/jsinspect ) can help you find duplicate
373+ [ jsinspect ] ( https://github.com/danielstjules/jsinspect ) can help you find duplicate
371374code eligible for refactoring.
372375
373376** Bad:**
@@ -741,11 +744,11 @@ class Airplane {
741744 getCruisingAltitude () {
742745 switch (this .type ) {
743746 case ' 777' :
744- return getMaxAltitude () - getPassengerCount ();
747+ return this . getMaxAltitude () - this . getPassengerCount ();
745748 case ' Air Force One' :
746- return getMaxAltitude ();
749+ return this . getMaxAltitude ();
747750 case ' Cessna' :
748- return getMaxAltitude () - getFuelExpenditure ();
751+ return this . getMaxAltitude () - this . getFuelExpenditure ();
749752 }
750753 }
751754}
@@ -760,21 +763,21 @@ class Airplane {
760763class Boeing777 extends Airplane {
761764 // ...
762765 getCruisingAltitude () {
763- return getMaxAltitude () - getPassengerCount ();
766+ return this . getMaxAltitude () - this . getPassengerCount ();
764767 }
765768}
766769
767770class AirForceOne extends Airplane {
768771 // ...
769772 getCruisingAltitude () {
770- return getMaxAltitude ();
773+ return this . getMaxAltitude ();
771774 }
772775}
773776
774777class Cessna extends Airplane {
775778 // ...
776779 getCruisingAltitude () {
777- return getMaxAltitude () - getFuelExpenditure ();
780+ return this . getMaxAltitude () - this . getFuelExpenditure ();
778781 }
779782}
780783```
@@ -819,12 +822,12 @@ TypeScript (which, like I said, is a great alternative!).
819822** Bad:**
820823``` javascript
821824function combine (val1 , val2 ) {
822- if (typeof val1 == " number" && typeof val2 == " number" ||
823- typeof val1 == " string" && typeof val2 == " string" ) {
825+ if (typeof val1 === ' number' && typeof val2 === ' number' ||
826+ typeof val1 === ' string' && typeof val2 === ' string' ) {
824827 return val1 + val2;
825- } else {
826- throw new Error (' Must be of type String or Number' );
827828 }
829+
830+ throw new Error (' Must be of type String or Number' );
828831}
829832```
830833
@@ -921,7 +924,7 @@ class BankAccount {
921924const bankAccount = new BankAccount ();
922925
923926// Buy shoes...
924- bankAccount .balance = bankAccount . balance - 100 ;
927+ bankAccount .balance -= 100 ;
925928```
926929
927930** Good** :
@@ -1002,12 +1005,12 @@ class UserSettings {
10021005 }
10031006
10041007 changeSettings (settings ) {
1005- if (this .verifyCredentials (user )) {
1008+ if (this .verifyCredentials ()) {
10061009 // ...
10071010 }
10081011 }
10091012
1010- verifyCredentials (user ) {
1013+ verifyCredentials () {
10111014 // ...
10121015 }
10131016}
@@ -1209,6 +1212,7 @@ function renderLargeShapes(shapes) {
12091212 switch (shape .constructor .name ) {
12101213 case ' Square' :
12111214 shape .setLength (5 );
1215+ break ;
12121216 case ' Rectangle' :
12131217 shape .setWidth (4 );
12141218 shape .setHeight (5 );
@@ -1321,6 +1325,16 @@ example below, the implicit contract is that any Request module for an
13211325
13221326** Bad:**
13231327``` javascript
1328+ class InventoryRequester {
1329+ constructor () {
1330+ this .REQ_METHODS = [' HTTP' ];
1331+ }
1332+
1333+ requestItem (item ) {
1334+ // ...
1335+ }
1336+ }
1337+
13241338class InventoryTracker {
13251339 constructor (items ) {
13261340 this .items = items;
@@ -1337,16 +1351,6 @@ class InventoryTracker {
13371351 }
13381352}
13391353
1340- class InventoryRequester {
1341- constructor () {
1342- this .REQ_METHODS = [' HTTP' ];
1343- }
1344-
1345- requestItem (item ) {
1346- // ...
1347- }
1348- }
1349-
13501354const inventoryTracker = new InventoryTracker ([' apples' , ' bananas' ]);
13511355inventoryTracker .requestItems ();
13521356```
@@ -1402,35 +1406,35 @@ classes until you find yourself needing larger and more complex objects.
14021406** Bad:**
14031407``` javascript
14041408const Animal = function (age ) {
1405- if (! (this instanceof Animal)) {
1406- throw new Error (" Instantiate Animal with `new`" );
1407- }
1409+ if (! (this instanceof Animal)) {
1410+ throw new Error (" Instantiate Animal with `new`" );
1411+ }
14081412
1409- this .age = age;
1413+ this .age = age;
14101414};
14111415
14121416Animal .prototype .move = function move () {};
14131417
14141418const Mammal = function (age , furColor ) {
1415- if (! (this instanceof Mammal)) {
1416- throw new Error (" Instantiate Mammal with `new`" );
1417- }
1419+ if (! (this instanceof Mammal)) {
1420+ throw new Error (" Instantiate Mammal with `new`" );
1421+ }
14181422
1419- Animal .call (this , age);
1420- this .furColor = furColor;
1423+ Animal .call (this , age);
1424+ this .furColor = furColor;
14211425};
14221426
14231427Mammal .prototype = Object .create (Animal .prototype );
14241428Mammal .prototype .constructor = Mammal;
14251429Mammal .prototype .liveBirth = function liveBirth () {};
14261430
14271431const Human = function (age , furColor , languageSpoken ) {
1428- if (! (this instanceof Human)) {
1429- throw new Error (" Instantiate Human with `new`" );
1430- }
1432+ if (! (this instanceof Human)) {
1433+ throw new Error (" Instantiate Human with `new`" );
1434+ }
14311435
1432- Mammal .call (this , age, furColor);
1433- this .languageSpoken = languageSpoken;
1436+ Mammal .call (this , age, furColor);
1437+ this .languageSpoken = languageSpoken;
14341438};
14351439
14361440Human .prototype = Object .create (Mammal .prototype );
@@ -1441,29 +1445,29 @@ Human.prototype.speak = function speak() {};
14411445** Good:**
14421446``` javascript
14431447class Animal {
1444- constructor (age ) {
1445- this .age = age;
1446- }
1448+ constructor (age ) {
1449+ this .age = age;
1450+ }
14471451
1448- move () {}
1452+ move () { /* ... */ }
14491453}
14501454
14511455class Mammal extends Animal {
1452- constructor (age , furColor ) {
1453- super (age);
1454- this .furColor = furColor;
1455- }
1456+ constructor (age , furColor ) {
1457+ super (age);
1458+ this .furColor = furColor;
1459+ }
14561460
1457- liveBirth () {}
1461+ liveBirth () { /* ... */ }
14581462}
14591463
14601464class Human extends Mammal {
1461- constructor (age , furColor , languageSpoken ) {
1462- super (age, furColor);
1463- this .languageSpoken = languageSpoken;
1464- }
1465+ constructor (age , furColor , languageSpoken ) {
1466+ super (age, furColor);
1467+ this .languageSpoken = languageSpoken;
1468+ }
14651469
1466- speak () {}
1470+ speak () { /* ... */ }
14671471}
14681472```
14691473** [ ⬆ back to top] ( #table-of-contents ) **
@@ -1596,6 +1600,15 @@ class EmployeeTaxData extends Employee {
15961600
15971601** Good** :
15981602``` javascript
1603+ class EmployeeTaxData {
1604+ constructor (ssn , salary ) {
1605+ this .ssn = ssn;
1606+ this .salary = salary;
1607+ }
1608+
1609+ // ...
1610+ }
1611+
15991612class Employee {
16001613 constructor (name , email ) {
16011614 this .name = name;
@@ -1608,15 +1621,6 @@ class Employee {
16081621 }
16091622 // ...
16101623}
1611-
1612- class EmployeeTaxData {
1613- constructor (ssn , salary ) {
1614- this .ssn = ssn;
1615- this .salary = salary;
1616- }
1617-
1618- // ...
1619- }
16201624```
16211625** [ ⬆ back to top] ( #table-of-contents ) **
16221626
@@ -1695,14 +1699,14 @@ Promises are a built-in global type. Use them!
16951699
16961700** Bad:**
16971701``` javascript
1698- require (' request' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' , (err , response ) => {
1699- if (err ) {
1700- console .error (err );
1702+ require (' request' ).get (' https://en.wikipedia.org/wiki/Robert_Cecil_Martin' , (requestErr , response ) => {
1703+ if (requestErr ) {
1704+ console .error (requestErr );
17011705 }
17021706 else {
1703- require (' fs' ).writeFile (' article.html' , response .body , (err ) => {
1704- if (err ) {
1705- console .error (err );
1707+ require (' fs' ).writeFile (' article.html' , response .body , (writeErr ) => {
1708+ if (writeErr ) {
1709+ console .error (writeErr );
17061710 } else {
17071711 console .log (' File written' );
17081712 }
@@ -1993,7 +1997,7 @@ function hashIt(data) {
19931997 // Make the hash
19941998 hash = ((hash << 5 ) - hash) + char;
19951999 // Convert to 32-bit integer
1996- hash = hash & hash;
2000+ hash &= hash;
19972001 }
19982002}
19992003```
@@ -2010,7 +2014,7 @@ function hashIt(data) {
20102014 hash = ((hash << 5 ) - hash) + char;
20112015
20122016 // Convert to 32-bit integer
2013- hash = hash & hash;
2017+ hash &= hash;
20142018 }
20152019}
20162020
0 commit comments