Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d279012

Browse files
authored
Merge branch 'master' into simplify
2 parents d7cc370 + 907617b commit d279012

File tree

1 file changed

+48
-109
lines changed

1 file changed

+48
-109
lines changed

README.md

Lines changed: 48 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Software engineering principles, from Robert C. Martin's book
2121
adapted for JavaScript. This is not a style guide. It's a guide to producing
2222
readable, reusable, and refactorable software in JavaScript.
2323

24-
Not every principle herein has to be strictly followed, and even less will be
24+
Not every principle herein has to be strictly followed, and even fewer will be
2525
universally agreed upon. These are guidelines and nothing more, but they are
2626
ones codified over many years of collective experience by the authors of
2727
*Clean Code*.
@@ -53,24 +53,6 @@ const yearMonthDay = moment().format('YYYY/MM/DD');
5353
```
5454
**[⬆ back to top](#table-of-contents)**
5555

56-
### Use ES2015/ES6 constants when variable values do not change
57-
In the bad example, the variable can be changed.
58-
When you declare a constant, the variable should stay
59-
the same throughout the program.
60-
61-
62-
**Bad:**
63-
```javascript
64-
var FIRST_US_PRESIDENT = "George Washington";
65-
```
66-
67-
**Good**:
68-
```javascript
69-
const FIRST_US_PRESIDENT = "George Washington";
70-
```
71-
**[⬆ back to top](#table-of-contents)**
72-
73-
7456
### Use the same vocabulary for the same type of variable
7557

7658
**Bad:**
@@ -136,9 +118,9 @@ const locations = ['Austin', 'New York', 'San Francisco'];
136118
locations.forEach((l) => {
137119
doStuff();
138120
doSomeOtherStuff();
139-
...
140-
...
141-
...
121+
// ...
122+
// ...
123+
// ...
142124
// Wait, what is `l` for again?
143125
dispatch(l);
144126
});
@@ -150,9 +132,9 @@ const locations = ['Austin', 'New York', 'San Francisco'];
150132
locations.forEach((location) => {
151133
doStuff();
152134
doSomeOtherStuff();
153-
...
154-
...
155-
...
135+
// ...
136+
// ...
137+
// ...
156138
dispatch(location);
157139
});
158140
```
@@ -212,7 +194,7 @@ function createMicrobrewery(name) {
212194
**[⬆ back to top](#table-of-contents)**
213195

214196
## **Functions**
215-
### Function arguments (2 or less ideally)
197+
### Function arguments (2 or fewer ideally)
216198
Limiting the amount of function parameters is incredibly important because it
217199
makes testing your function easier. Having more than three leads to a
218200
combinatorial explosion where you have to test tons of different cases with
@@ -231,7 +213,7 @@ lot of arguments.
231213
**Bad:**
232214
```javascript
233215
function createMenu(title, body, buttonText, cancellable) {
234-
...
216+
// ...
235217
}
236218
```
237219

@@ -245,7 +227,7 @@ const menuConfig = {
245227
}
246228

247229
function createMenu(menuConfig) {
248-
...
230+
// ...
249231
}
250232

251233
```
@@ -353,7 +335,7 @@ function tokenize(code) {
353335
const tokens = [];
354336
REGEXES.forEach((REGEX) => {
355337
statements.forEach((statement) => {
356-
tokens.push( // ... );
338+
tokens.push( /* ... */ );
357339
})
358340
});
359341

@@ -363,7 +345,7 @@ function tokenize(code) {
363345
function lexer(tokens) {
364346
const ast = [];
365347
tokens.forEach((token) => {
366-
ast.push( // ... );
348+
ast.push( /* ... */ );
367349
});
368350

369351
return ast;
@@ -391,7 +373,7 @@ code eligible for refactoring.
391373
**Bad:**
392374
```javascript
393375
function showDeveloperList(developers) {
394-
developers.forEach(developers => {
376+
developers.forEach(developer => {
395377
const expectedSalary = developer.calculateExpectedSalary();
396378
const experience = developer.getExperience();
397379
const githubLink = developer.getGithubLink();
@@ -459,7 +441,7 @@ function writeForumComment(subject, body) {
459441
**Good**:
460442
```javascript
461443
function writeForumComment(subject = 'No subject', body = 'No text') {
462-
...
444+
// ...
463445
}
464446

465447
```
@@ -529,13 +511,13 @@ function createFile(name, temp) {
529511

530512
**Good**:
531513
```javascript
532-
function createTempFile(name) {
533-
fs.create('./temp/' + name);
534-
}
535-
536514
function createFile(name) {
537515
fs.create(name);
538516
}
517+
518+
function createTempFile(name) {
519+
createFile('./temp/' + name);
520+
}
539521
```
540522
**[⬆ back to top](#table-of-contents)**
541523

@@ -601,11 +583,11 @@ Array.prototype.diff = function(comparisonArray) {
601583
const values = [];
602584
const hash = {};
603585

604-
for (let i of comparisonArray) {
586+
for (const i of comparisonArray) {
605587
hash[i] = true;
606588
}
607589

608-
for (let i of this) {
590+
for (const i of this) {
609591
if (!hash[i]) {
610592
values.push(i);
611593
}
@@ -626,7 +608,6 @@ class SuperArray extends Array {
626608
**[⬆ back to top](#table-of-contents)**
627609

628610
### Favor functional programming over imperative programming
629-
If Haskell were an IPA then JavaScript would be an O'Douls. That is to say,
630611
JavaScript isn't a functional language in the way that Haskell is, but it has
631612
a functional flavor to it. Functional languages are cleaner and easier to test.
632613
Favor this style of programming when you can.
@@ -685,7 +666,7 @@ const totalOutput = programmerOutput
685666
**Bad:**
686667
```javascript
687668
if (fsm.state === 'fetching' && isEmpty(listNode)) {
688-
/// ...
669+
// ...
689670
}
690671
```
691672

@@ -739,7 +720,7 @@ just do one thing.
739720
**Bad:**
740721
```javascript
741722
class Airplane {
742-
//...
723+
// ...
743724
getCruisingAltitude() {
744725
switch (this.type) {
745726
case '777':
@@ -756,25 +737,25 @@ class Airplane {
756737
**Good**:
757738
```javascript
758739
class Airplane {
759-
//...
740+
// ...
760741
}
761742

762743
class Boeing777 extends Airplane {
763-
//...
744+
// ...
764745
getCruisingAltitude() {
765746
return getMaxAltitude() - getPassengerCount();
766747
}
767748
}
768749

769750
class AirForceOne extends Airplane {
770-
//...
751+
// ...
771752
getCruisingAltitude() {
772753
return getMaxAltitude();
773754
}
774755
}
775756

776757
class Cessna extends Airplane {
777-
//...
758+
// ...
778759
getCruisingAltitude() {
779760
return getMaxAltitude() - getFuelExpenditure();
780761
}
@@ -814,7 +795,7 @@ you should consider using TypeScript. It is an excellent alternative to normal
814795
JavaScript, as it provides you with static typing on top of standard JavaScript
815796
syntax. The problem with manually type-checking normal JavaScript is that
816797
doing it well requires so much extra verbiage that the faux "type-safety" you get
817-
doesn't make up for the lost readability. Keep your JavaScript, clean, write
798+
doesn't make up for the lost readability. Keep your JavaScript clean, write
818799
good tests, and have good code reviews. Otherwise, do all of that but with
819800
TypeScript (which, like I said, is a great alternative!).
820801

@@ -848,8 +829,8 @@ they are fixed if they can be.
848829
**Bad:**
849830
```javascript
850831

851-
// On old browsers, each iteration would be costly because `len` would be
852-
// recomputed. In modern browsers, this is optimized.
832+
// On old browsers, each iteration with uncached `list.length` would be costly
833+
// because of `list.length` recomputation. In modern browsers, this is optimized.
853834
for (let i = 0, len = list.length; i < len; i++) {
854835
// ...
855836
}
@@ -916,7 +897,7 @@ server.
916897
```javascript
917898
class BankAccount {
918899
constructor() {
919-
this.balance = 1000;
900+
this.balance = 1000;
920901
}
921902
}
922903

@@ -930,14 +911,14 @@ bankAccount.balance = bankAccount.balance - 100;
930911
```javascript
931912
class BankAccount {
932913
constructor() {
933-
this.balance = 1000;
914+
this.balance = 1000;
934915
}
935916

936917
// It doesn't have to be prefixed with `get` or `set` to be a getter/setter
937918
withdraw(amount) {
938-
if (verifyAmountCanBeDeducted(amount)) {
939-
this.balance -= amount;
940-
}
919+
if (verifyAmountCanBeDeducted(amount)) {
920+
this.balance -= amount;
921+
}
941922
}
942923
}
943924

@@ -1148,12 +1129,12 @@ function renderLargeRectangles(rectangles) {
11481129
rectangles.forEach((rectangle) => {
11491130
rectangle.setWidth(4);
11501131
rectangle.setHeight(5);
1151-
let area = rectangle.getArea(); // BAD: Will return 25 for Square. Should be 20.
1132+
const area = rectangle.getArea(); // BAD: Will return 25 for Square. Should be 20.
11521133
rectangle.render(area);
11531134
})
11541135
}
11551136

1156-
let rectangles = [new Rectangle(), new Rectangle(), new Square()];
1137+
const rectangles = [new Rectangle(), new Rectangle(), new Square()];
11571138
renderLargeRectangles(rectangles);
11581139
```
11591140

@@ -1488,7 +1469,7 @@ class Car {
14881469
}
14891470

14901471
setMake(make) {
1491-
this.name = name;
1472+
this.make = make;
14921473
}
14931474

14941475
setModel(model) {
@@ -1521,7 +1502,7 @@ class Car {
15211502
}
15221503

15231504
setMake(make) {
1524-
this.name = name;
1505+
this.make = make;
15251506
// NOTE: Returning this for chaining
15261507
return this;
15271508
}
@@ -1914,9 +1895,9 @@ class PerformanceReview {
19141895
}
19151896

19161897
perfReview() {
1917-
getPeerReviews();
1918-
getManagerReview();
1919-
getSelfReview();
1898+
this.getPeerReviews();
1899+
this.getManagerReview();
1900+
this.getSelfReview();
19201901
}
19211902

19221903
getManagerReview() {
@@ -1928,7 +1909,7 @@ class PerformanceReview {
19281909
}
19291910
}
19301911

1931-
let review = new PerformanceReview(user);
1912+
const review = new PerformanceReview(user);
19321913
review.perfReview();
19331914
```
19341915

@@ -1940,9 +1921,9 @@ class PerformanceReview {
19401921
}
19411922

19421923
perfReview() {
1943-
getPeerReviews();
1944-
getManagerReview();
1945-
getSelfReview();
1924+
this.getPeerReviews();
1925+
this.getManagerReview();
1926+
this.getSelfReview();
19461927
}
19471928

19481929
getPeerReviews() {
@@ -1967,7 +1948,7 @@ class PerformanceReview {
19671948
}
19681949
}
19691950

1970-
let review = new PerformanceReview(employee);
1951+
const review = new PerformanceReview(employee);
19711952
review.perfReview();
19721953
```
19731954

@@ -2068,7 +2049,7 @@ proper indentation and formatting give the visual structure to your code.
20682049
////////////////////////////////////////////////////////////////////////////////
20692050
// Scope Model Instantiation
20702051
////////////////////////////////////////////////////////////////////////////////
2071-
const $scope.model = {
2052+
$scope.model = {
20722053
menu: 'foo',
20732054
nav: 'bar'
20742055
};
@@ -2083,7 +2064,7 @@ const actions = function() {
20832064

20842065
**Good**:
20852066
```javascript
2086-
const $scope.model = {
2067+
$scope.model = {
20872068
menu: 'foo',
20882069
nav: 'bar'
20892070
};
@@ -2093,45 +2074,3 @@ const actions = function() {
20932074
}
20942075
```
20952076
**[⬆ back to top](#table-of-contents)**
2096-
2097-
### Avoid legal comments in source files
2098-
That's what your `LICENSE` file at the top of your source tree is for.
2099-
2100-
**Bad:**
2101-
```javascript
2102-
/*
2103-
The MIT License (MIT)
2104-
2105-
Copyright (c) 2016 Ryan McDermott
2106-
2107-
Permission is hereby granted, free of charge, to any person obtaining a copy
2108-
of this software and associated documentation files (the "Software"), to deal
2109-
in the Software without restriction, including without limitation the rights
2110-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2111-
copies of the Software, and to permit persons to whom the Software is
2112-
furnished to do so, subject to the following conditions:
2113-
2114-
The above copyright notice and this permission notice shall be included in all
2115-
copies or substantial portions of the Software.
2116-
2117-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2118-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2119-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2120-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2121-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2122-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2123-
SOFTWARE
2124-
*/
2125-
2126-
function calculateBill() {
2127-
// ...
2128-
}
2129-
```
2130-
2131-
**Good**:
2132-
```javascript
2133-
function calculateBill() {
2134-
// ...
2135-
}
2136-
```
2137-
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)