Skip to content

Commit 0b55872

Browse files
authored
Merge branch 'master' into single-quotes
2 parents c92b792 + fcfab4e commit 0b55872

File tree

1 file changed

+75
-71
lines changed

1 file changed

+75
-71
lines changed

README.md

+75-71
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ getUser();
7272
We will read more code than we will ever write. It's important that the code we
7373
do write is readable and searchable. By *not* naming variables that end up
7474
being 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
367370
developer. Duplicate code means there's more than one place to alter something
368371
if you need to change some logic. JavaScript is untyped, so it makes having
369372
generic 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
371374
code 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 {
760763
class Boeing777 extends Airplane {
761764
// ...
762765
getCruisingAltitude() {
763-
return getMaxAltitude() - getPassengerCount();
766+
return this.getMaxAltitude() - this.getPassengerCount();
764767
}
765768
}
766769

767770
class AirForceOne extends Airplane {
768771
// ...
769772
getCruisingAltitude() {
770-
return getMaxAltitude();
773+
return this.getMaxAltitude();
771774
}
772775
}
773776

774777
class 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
821824
function 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 {
921924
const bankAccount = new BankAccount();
922925

923926
// Buy shoes...
924-
bankAccount.balance = bankAccount.balance - 100;
927+
bankAccount.balance -= 100;
925928
```
926929

927930
**Good**:
@@ -1006,12 +1009,12 @@ class UserSettings {
10061009
}
10071010

10081011
changeSettings(settings) {
1009-
if (this.verifyCredentials(user)) {
1012+
if (this.verifyCredentials()) {
10101013
// ...
10111014
}
10121015
}
10131016

1014-
verifyCredentials(user) {
1017+
verifyCredentials() {
10151018
// ...
10161019
}
10171020
}
@@ -1213,6 +1216,7 @@ function renderLargeShapes(shapes) {
12131216
switch (shape.constructor.name) {
12141217
case 'Square':
12151218
shape.setLength(5);
1219+
break;
12161220
case 'Rectangle':
12171221
shape.setWidth(4);
12181222
shape.setHeight(5);
@@ -1325,6 +1329,16 @@ example below, the implicit contract is that any Request module for an
13251329

13261330
**Bad:**
13271331
```javascript
1332+
class InventoryRequester {
1333+
constructor() {
1334+
this.REQ_METHODS = ['HTTP'];
1335+
}
1336+
1337+
requestItem(item) {
1338+
// ...
1339+
}
1340+
}
1341+
13281342
class InventoryTracker {
13291343
constructor(items) {
13301344
this.items = items;
@@ -1341,16 +1355,6 @@ class InventoryTracker {
13411355
}
13421356
}
13431357

1344-
class InventoryRequester {
1345-
constructor() {
1346-
this.REQ_METHODS = ['HTTP'];
1347-
}
1348-
1349-
requestItem(item) {
1350-
// ...
1351-
}
1352-
}
1353-
13541358
const inventoryTracker = new InventoryTracker(['apples', 'bananas']);
13551359
inventoryTracker.requestItems();
13561360
```
@@ -1406,35 +1410,35 @@ classes until you find yourself needing larger and more complex objects.
14061410
**Bad:**
14071411
```javascript
14081412
const Animal = function(age) {
1409-
if (!(this instanceof Animal)) {
1410-
throw new Error('Instantiate Animal with `new`');
1411-
}
1413+
if (!(this instanceof Animal)) {
1414+
throw new Error('Instantiate Animal with `new`');
1415+
}
14121416

1413-
this.age = age;
1417+
this.age = age;
14141418
};
14151419

14161420
Animal.prototype.move = function() {};
14171421

14181422
const Mammal = function(age, furColor) {
1419-
if (!(this instanceof Mammal)) {
1420-
throw new Error('Instantiate Mammal with `new`');
1421-
}
1423+
if (!(this instanceof Mammal)) {
1424+
throw new Error('Instantiate Mammal with `new`');
1425+
}
14221426

1423-
Animal.call(this, age);
1424-
this.furColor = furColor;
1427+
Animal.call(this, age);
1428+
this.furColor = furColor;
14251429
};
14261430

14271431
Mammal.prototype = Object.create(Animal.prototype);
14281432
Mammal.prototype.constructor = Mammal;
14291433
Mammal.prototype.liveBirth = function() {};
14301434

14311435
const Human = function(age, furColor, languageSpoken) {
1432-
if (!(this instanceof Human)) {
1433-
throw new Error('Instantiate Human with `new`');
1434-
}
1436+
if (!(this instanceof Human)) {
1437+
throw new Error('Instantiate Human with `new`');
1438+
}
14351439

1436-
Mammal.call(this, age, furColor);
1437-
this.languageSpoken = languageSpoken;
1440+
Mammal.call(this, age, furColor);
1441+
this.languageSpoken = languageSpoken;
14381442
};
14391443

14401444
Human.prototype = Object.create(Mammal.prototype);
@@ -1445,29 +1449,29 @@ Human.prototype.speak = function() {};
14451449
**Good:**
14461450
```javascript
14471451
class Animal {
1448-
constructor(age) {
1449-
this.age = age;
1450-
}
1452+
constructor(age) {
1453+
this.age = age;
1454+
}
14511455

1452-
move() {}
1456+
move() { /* ... */ }
14531457
}
14541458

14551459
class Mammal extends Animal {
1456-
constructor(age, furColor) {
1457-
super(age);
1458-
this.furColor = furColor;
1459-
}
1460+
constructor(age, furColor) {
1461+
super(age);
1462+
this.furColor = furColor;
1463+
}
14601464

1461-
liveBirth() {}
1465+
liveBirth() { /* ... */ }
14621466
}
14631467

14641468
class Human extends Mammal {
1465-
constructor(age, furColor, languageSpoken) {
1466-
super(age, furColor);
1467-
this.languageSpoken = languageSpoken;
1468-
}
1469+
constructor(age, furColor, languageSpoken) {
1470+
super(age, furColor);
1471+
this.languageSpoken = languageSpoken;
1472+
}
14691473

1470-
speak() {}
1474+
speak() { /* ... */ }
14711475
}
14721476
```
14731477
**[⬆ back to top](#table-of-contents)**
@@ -1600,6 +1604,15 @@ class EmployeeTaxData extends Employee {
16001604

16011605
**Good**:
16021606
```javascript
1607+
class EmployeeTaxData {
1608+
constructor(ssn, salary) {
1609+
this.ssn = ssn;
1610+
this.salary = salary;
1611+
}
1612+
1613+
// ...
1614+
}
1615+
16031616
class Employee {
16041617
constructor(name, email) {
16051618
this.name = name;
@@ -1612,15 +1625,6 @@ class Employee {
16121625
}
16131626
// ...
16141627
}
1615-
1616-
class EmployeeTaxData {
1617-
constructor(ssn, salary) {
1618-
this.ssn = ssn;
1619-
this.salary = salary;
1620-
}
1621-
1622-
// ...
1623-
}
16241628
```
16251629
**[⬆ back to top](#table-of-contents)**
16261630

@@ -1699,14 +1703,14 @@ Promises are a built-in global type. Use them!
16991703

17001704
**Bad:**
17011705
```javascript
1702-
require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', function(err, response) {
1703-
if (err) {
1704-
console.error(err);
1706+
require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', (requestErr, response) => {
1707+
if (requestErr) {
1708+
console.error(requestErr);
17051709
}
17061710
else {
1707-
require('fs').writeFile('article.html', response.body, function(err) {
1708-
if (err) {
1709-
console.error(err);
1711+
require('fs').writeFile('article.html', response.body, (writeErr) => {
1712+
if (writeErr) {
1713+
console.error(writeErr);
17101714
} else {
17111715
console.log('File written');
17121716
}
@@ -1997,7 +2001,7 @@ function hashIt(data) {
19972001
// Make the hash
19982002
hash = ((hash << 5) - hash) + char;
19992003
// Convert to 32-bit integer
2000-
hash = hash & hash;
2004+
hash &= hash;
20012005
}
20022006
}
20032007
```
@@ -2014,7 +2018,7 @@ function hashIt(data) {
20142018
hash = ((hash << 5) - hash) + char;
20152019

20162020
// Convert to 32-bit integer
2017-
hash = hash & hash;
2021+
hash &= hash;
20182022
}
20192023
}
20202024

0 commit comments

Comments
 (0)