Skip to content

Commit

Permalink
Merge branch 'master' into single-quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
vsemozhetbyt authored Jan 10, 2017
2 parents c92b792 + fcfab4e commit 0b55872
Showing 1 changed file with 75 additions and 71 deletions.
146 changes: 75 additions & 71 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ getUser();
We will read more code than we will ever write. It's important that the code we
do write is readable and searchable. By *not* naming variables that end up
being meaningful for understanding our program, we hurt our readers.
Make your names searchable.
Make your names searchable. Tools like
[buddy.js](https://github.com/danielstjules/buddy.js) and
[ESLint](https://github.com/eslint/eslint/blob/660e0918933e6e7fede26bc675a0763a6b357c94/docs/rules/no-magic-numbers.md)
can help identify unnamed constants.

**Bad:**
```javascript
Expand Down Expand Up @@ -226,7 +229,7 @@ const menuConfig = {
cancellable: true
}
function createMenu(menuConfig) {
function createMenu(config) {
// ...
}
Expand Down Expand Up @@ -367,7 +370,7 @@ for it and it's quite possibly the worst sin you can commit as a professional
developer. Duplicate code means there's more than one place to alter something
if you need to change some logic. JavaScript is untyped, so it makes having
generic functions quite easy. Take advantage of that! Tools like
[jsinpect](https://github.com/danielstjules/jsinspect) can help you find duplicate
[jsinspect](https://github.com/danielstjules/jsinspect) can help you find duplicate
code eligible for refactoring.

**Bad:**
Expand Down Expand Up @@ -741,11 +744,11 @@ class Airplane {
getCruisingAltitude() {
switch (this.type) {
case '777':
return getMaxAltitude() - getPassengerCount();
return this.getMaxAltitude() - this.getPassengerCount();
case 'Air Force One':
return getMaxAltitude();
return this.getMaxAltitude();
case 'Cessna':
return getMaxAltitude() - getFuelExpenditure();
return this.getMaxAltitude() - this.getFuelExpenditure();
}
}
}
Expand All @@ -760,21 +763,21 @@ class Airplane {
class Boeing777 extends Airplane {
// ...
getCruisingAltitude() {
return getMaxAltitude() - getPassengerCount();
return this.getMaxAltitude() - this.getPassengerCount();
}
}
class AirForceOne extends Airplane {
// ...
getCruisingAltitude() {
return getMaxAltitude();
return this.getMaxAltitude();
}
}
class Cessna extends Airplane {
// ...
getCruisingAltitude() {
return getMaxAltitude() - getFuelExpenditure();
return this.getMaxAltitude() - this.getFuelExpenditure();
}
}
```
Expand Down Expand Up @@ -819,12 +822,12 @@ TypeScript (which, like I said, is a great alternative!).
**Bad:**
```javascript
function combine(val1, val2) {
if (typeof val1 == 'number' && typeof val2 == 'number' ||
typeof val1 == 'string' && typeof val2 == 'string') {
if (typeof val1 === 'number' && typeof val2 === 'number' ||
typeof val1 === 'string' && typeof val2 === 'string') {
return val1 + val2;
} else {
throw new Error('Must be of type String or Number');
}
throw new Error('Must be of type String or Number');
}
```

Expand Down Expand Up @@ -921,7 +924,7 @@ class BankAccount {
const bankAccount = new BankAccount();
// Buy shoes...
bankAccount.balance = bankAccount.balance - 100;
bankAccount.balance -= 100;
```

**Good**:
Expand Down Expand Up @@ -1006,12 +1009,12 @@ class UserSettings {
}
changeSettings(settings) {
if (this.verifyCredentials(user)) {
if (this.verifyCredentials()) {
// ...
}
}
verifyCredentials(user) {
verifyCredentials() {
// ...
}
}
Expand Down Expand Up @@ -1213,6 +1216,7 @@ function renderLargeShapes(shapes) {
switch (shape.constructor.name) {
case 'Square':
shape.setLength(5);
break;
case 'Rectangle':
shape.setWidth(4);
shape.setHeight(5);
Expand Down Expand Up @@ -1325,6 +1329,16 @@ example below, the implicit contract is that any Request module for an
**Bad:**
```javascript
class InventoryRequester {
constructor() {
this.REQ_METHODS = ['HTTP'];
}

requestItem(item) {
// ...
}
}

class InventoryTracker {
constructor(items) {
this.items = items;
Expand All @@ -1341,16 +1355,6 @@ class InventoryTracker {
}
}

class InventoryRequester {
constructor() {
this.REQ_METHODS = ['HTTP'];
}

requestItem(item) {
// ...
}
}

const inventoryTracker = new InventoryTracker(['apples', 'bananas']);
inventoryTracker.requestItems();
```
Expand Down Expand Up @@ -1406,35 +1410,35 @@ classes until you find yourself needing larger and more complex objects.
**Bad:**
```javascript
const Animal = function(age) {
if (!(this instanceof Animal)) {
throw new Error('Instantiate Animal with `new`');
}
if (!(this instanceof Animal)) {
throw new Error('Instantiate Animal with `new`');
}

this.age = age;
this.age = age;
};

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

const Mammal = function(age, furColor) {
if (!(this instanceof Mammal)) {
throw new Error('Instantiate Mammal with `new`');
}
if (!(this instanceof Mammal)) {
throw new Error('Instantiate Mammal with `new`');
}

Animal.call(this, age);
this.furColor = furColor;
Animal.call(this, age);
this.furColor = furColor;
};

Mammal.prototype = Object.create(Animal.prototype);
Mammal.prototype.constructor = Mammal;
Mammal.prototype.liveBirth = function() {};

const Human = function(age, furColor, languageSpoken) {
if (!(this instanceof Human)) {
throw new Error('Instantiate Human with `new`');
}
if (!(this instanceof Human)) {
throw new Error('Instantiate Human with `new`');
}

Mammal.call(this, age, furColor);
this.languageSpoken = languageSpoken;
Mammal.call(this, age, furColor);
this.languageSpoken = languageSpoken;
};

Human.prototype = Object.create(Mammal.prototype);
Expand All @@ -1445,29 +1449,29 @@ Human.prototype.speak = function() {};
**Good:**
```javascript
class Animal {
constructor(age) {
this.age = age;
}
constructor(age) {
this.age = age;
}

move() {}
move() { /* ... */ }
}

class Mammal extends Animal {
constructor(age, furColor) {
super(age);
this.furColor = furColor;
}
constructor(age, furColor) {
super(age);
this.furColor = furColor;
}

liveBirth() {}
liveBirth() { /* ... */ }
}

class Human extends Mammal {
constructor(age, furColor, languageSpoken) {
super(age, furColor);
this.languageSpoken = languageSpoken;
}
constructor(age, furColor, languageSpoken) {
super(age, furColor);
this.languageSpoken = languageSpoken;
}

speak() {}
speak() { /* ... */ }
}
```
**[⬆ back to top](#table-of-contents)**
Expand Down Expand Up @@ -1600,6 +1604,15 @@ class EmployeeTaxData extends Employee {
**Good**:
```javascript
class EmployeeTaxData {
constructor(ssn, salary) {
this.ssn = ssn;
this.salary = salary;
}

// ...
}

class Employee {
constructor(name, email) {
this.name = name;
Expand All @@ -1612,15 +1625,6 @@ class Employee {
}
// ...
}

class EmployeeTaxData {
constructor(ssn, salary) {
this.ssn = ssn;
this.salary = salary;
}

// ...
}
```
**[⬆ back to top](#table-of-contents)**
Expand Down Expand Up @@ -1699,14 +1703,14 @@ Promises are a built-in global type. Use them!
**Bad:**
```javascript
require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', function(err, response) {
if (err) {
console.error(err);
require('request').get('https://en.wikipedia.org/wiki/Robert_Cecil_Martin', (requestErr, response) => {
if (requestErr) {
console.error(requestErr);
}
else {
require('fs').writeFile('article.html', response.body, function(err) {
if (err) {
console.error(err);
require('fs').writeFile('article.html', response.body, (writeErr) => {
if (writeErr) {
console.error(writeErr);
} else {
console.log('File written');
}
Expand Down Expand Up @@ -1997,7 +2001,7 @@ function hashIt(data) {
// Make the hash
hash = ((hash << 5) - hash) + char;
// Convert to 32-bit integer
hash = hash & hash;
hash &= hash;
}
}
```
Expand All @@ -2014,7 +2018,7 @@ function hashIt(data) {
hash = ((hash << 5) - hash) + char;

// Convert to 32-bit integer
hash = hash & hash;
hash &= hash;
}
}

Expand Down

0 comments on commit 0b55872

Please sign in to comment.