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
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Copy file name to clipboardExpand all lines: javascript/README.md
+93-35Lines changed: 93 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,9 +7,9 @@ Please comment code extensively. More comments are always better than fewer comm
7
7
// bad!
8
8
// Shows footer if shopping bag is absent
9
9
var showFooter =!$('.shopping-bag').length;
10
-
10
+
11
11
// good
12
-
// PROJ-12: As per client request, we hide the footer if
12
+
// PROJ-12: As per client request, we hide the footer if
13
13
// the shopping bag is not visible on the page.
14
14
var showFooter =!$('.shopping-bag').length;
15
15
````
@@ -37,14 +37,14 @@ In general, always favour comment placement that leads to less indenting.
37
37
Don't commit commented out sections of code back into the repository. Just delete the code. That's what git's history is for!
38
38
39
39
If a piece of code is very temporarily being removed, and will be reinserted shortly, you might decide to do this anyway. Please leave a detailed comment explaining exactly why. Sorta like those post-its we leave on boxes in the fridge with our name and date so that we know when to chuck 'em out!
40
-
40
+
41
41
42
42
##Use single quotes
43
43
44
44
````javascript
45
45
// good
46
46
$('.footer')
47
-
47
+
48
48
// bad
49
49
$(".footer")
50
50
````
@@ -54,14 +54,14 @@ $(".footer")
54
54
````javascript
55
55
// bad: what was 3 again? Text node? Comment?
56
56
if (el.nodeType===3) { ... }
57
-
57
+
58
58
// bad: The reader doesn't know why we chose 7, and if we change 7 with 8, we'll have to carefully search and replace all occurrences
59
59
if ($('.blah').length===7) { ... }
60
-
60
+
61
61
// good
62
-
62
+
63
63
if (el.nodeType===Node.TEXT_NODE) { ... }
64
-
64
+
65
65
// good
66
66
if ($('.blah').length=== defaultRoomCount) { ... }
67
67
````
@@ -73,14 +73,14 @@ Separate declarations with a semicolon and a line break.
73
73
````javascript
74
74
// bad: these are all in the global scope and is hard to read
75
75
items = {}, length =items.length, name ='foo';
76
-
76
+
77
77
// bad: if you comment out the first line, the subsequent declarations
78
78
// become global variables
79
79
// additionally, these do not beautify in DevTools nicely (semi-colons do)
80
80
var items = {},
81
81
length =items.length,
82
82
name ='foo';
83
-
83
+
84
84
// good: easiest to read/edit, and because we run our code through uglify,
85
85
// it will be optimized at compile-time anyways
86
86
var items = {};
@@ -118,7 +118,7 @@ function foo () {
118
118
...
119
119
}
120
120
121
-
// good
121
+
// good
122
122
varfoo=function() {
123
123
...
124
124
}
@@ -131,7 +131,7 @@ But no space before the opening argument parenthesis.
131
131
````javascript
132
132
// bad
133
133
varfoo=function(){...}
134
-
134
+
135
135
// good: whitespace improves readability here
136
136
varfoo=function() {
137
137
...
@@ -143,7 +143,7 @@ var foo = function() {
143
143
````javascript
144
144
// bad: we want to differentiate between operations (see below) and function calls
145
145
doStuff ();
146
-
146
+
147
147
// good
148
148
doStuff();
149
149
````
@@ -153,7 +153,7 @@ doStuff();
153
153
````javascript
154
154
// bad: we are recalculating items.length every time
155
155
for (var i =0; i <items.length; i++)
156
-
156
+
157
157
// good
158
158
for (var i =0, length =items.length; i < length; i++)
159
159
````
@@ -163,7 +163,7 @@ for (var i = 0, length = items.length; i < length; i++)
163
163
````javascript
164
164
// bad: we want to differentiate between function calls (see above) and conditions
165
165
if(true)
166
-
166
+
167
167
// good
168
168
if (true)
169
169
````
@@ -173,7 +173,7 @@ if (true)
173
173
````javascript
174
174
// bad
175
175
var c=a+b;
176
-
176
+
177
177
// good: yay whitespace!
178
178
var c = a + b;
179
179
````
@@ -188,7 +188,7 @@ if (true) {
188
188
else {
189
189
...
190
190
}
191
-
191
+
192
192
// good
193
193
if (true) {
194
194
...
@@ -203,7 +203,7 @@ if (true) {
203
203
// bad
204
204
'footer':{
205
205
}
206
-
206
+
207
207
// good
208
208
'footer': {
209
209
}
@@ -214,7 +214,7 @@ if (true) {
214
214
````javascript
215
215
// bad: it's more work to add a line to this block
216
216
if (isVisible) returntrue;
217
-
217
+
218
218
// good: this will get minified anyways
219
219
if (isVisible) {
220
220
returntrue;
@@ -237,11 +237,11 @@ Use a descriptive variable name instead.
237
237
````javascript
238
238
// bad: what is this supposed to be?
239
239
var arr = [];
240
-
240
+
241
241
// bad: why is the object type so important that it needs to be in the name?
242
242
var itemsArray = [];
243
243
var computerObj = {};
244
-
244
+
245
245
// good
246
246
var items = [];
247
247
var computer = {};
@@ -252,10 +252,10 @@ var computer = {};
252
252
````javascript
253
253
// bad: underscores shouldn’t be used in identifiers
254
254
functionrecalculate_item_height() { ... }
255
-
255
+
256
256
// bad: this is our constructor name convention
257
257
functionRecalculateItemHeight() { ... }
258
-
258
+
259
259
// good
260
260
functionrecalculateItemHeight() { ... }
261
261
````
@@ -265,7 +265,7 @@ function recalculateItemHeight() { ... }
265
265
````javascript
266
266
// bad
267
267
var is_visible =true;
268
-
268
+
269
269
// good
270
270
var isVisible =true;
271
271
````
@@ -288,14 +288,14 @@ var router = new Router();
288
288
functionawesomemovie(options) {
289
289
this.title=options.title;
290
290
}
291
-
291
+
292
292
var titanic =newawesomemovie({ title:'Titanic' });
293
-
293
+
294
294
// good
295
295
functionAwesomeMovie(options) {
296
296
this.title=options.title;
297
297
}
298
-
298
+
299
299
var fiftyShades =newAwesomeMovie({ title:'50 Shades Of Grey' });
300
300
````
301
301
@@ -324,23 +324,23 @@ var Utils = {
324
324
functionBadExample(name, value) {
325
325
this.name= name;
326
326
this.value= value;
327
-
327
+
328
328
this.doSomeStuff=function() {
329
329
// some stuff done
330
330
}
331
331
}
332
-
332
+
333
333
// good
334
334
// methods declared with the prototype will be shared across all instances, reducing memory footprint
0 commit comments