You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
JavaScript is untyped, so capitalization tells you a lot about your variables,
@@ -123,6 +125,7 @@ function restoreDatabase() {}
123
125
classAnimal {}
124
126
classAlpaca {}
125
127
```
128
+
**[⬆ back to top](#table-of-contents)**
126
129
127
130
### Don't add unneeded context
128
131
If your class/object name tells you something, don't repeat that in your
@@ -153,6 +156,7 @@ function paintCar(car) {
153
156
car.color='Red';
154
157
}
155
158
```
159
+
**[⬆ back to top](#table-of-contents)**
156
160
157
161
## **Functions**
158
162
### Limit the amount of function parameters (2 or less)
@@ -179,6 +183,7 @@ function createMenu(config) {
179
183
}
180
184
181
185
```
186
+
**[⬆ back to top](#table-of-contents)**
182
187
183
188
### Use default arguments instead of short circuiting
184
189
**Bad:**
@@ -187,6 +192,7 @@ function writeForumComment(subject, body) {
187
192
subject = subject ||'No Subject';
188
193
body = body ||'No text';
189
194
}
195
+
190
196
```
191
197
192
198
**Good**:
@@ -196,6 +202,7 @@ function writeForumComment(subject='No subject', body='No text') {
196
202
}
197
203
198
204
```
205
+
**[⬆ back to top](#table-of-contents)**
199
206
200
207
### Don't use flags as function parameters
201
208
Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean.
@@ -209,7 +216,6 @@ function createFile(name, temp) {
209
216
fs.create(name);
210
217
}
211
218
}
212
-
213
219
```
214
220
215
221
**Good**:
@@ -222,6 +228,7 @@ function createFile(name) {
222
228
fs.create(name);
223
229
}
224
230
```
231
+
**[⬆ back to top](#table-of-contents)**
225
232
226
233
### Avoid Side Effects
227
234
A function produces a side effect if it does anything other than take a value in
@@ -255,6 +262,7 @@ var newName = splitIntoFirstAndLastName(name);
255
262
console.log(name); // 'Ryan McDermott';
256
263
console.log(newName); // ['Ryan', 'McDermott'];
257
264
```
265
+
**[⬆ back to top](#table-of-contents)**
258
266
259
267
### Don't write to global functions
260
268
Polluting globals is a bad practice in JavaScript because you could clash with another
@@ -312,6 +320,7 @@ class SuperArray extends Array {
312
320
}
313
321
}
314
322
```
323
+
**[⬆ back to top](#table-of-contents)**
315
324
316
325
## **Classes**
317
326
### Prefer ES6 classes over ES5 plain functions
@@ -387,6 +396,7 @@ class Human extends Mammal {
387
396
speak() {}
388
397
}
389
398
```
399
+
**[⬆ back to top](#table-of-contents)**
390
400
391
401
## **Comments**
392
402
### Only comment things that have business logic complexity.
@@ -430,6 +440,7 @@ function hashIt(data) {
430
440
}
431
441
432
442
```
443
+
**[⬆ back to top](#table-of-contents)**
433
444
434
445
### Don't leave commented code in your codebase
435
446
Version control exists for a reason. Leave old code in your history.
0 commit comments