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: README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ A repo to document code standards for different languages and provide tools for
6
6
7
7
## JavaScript
8
8
9
-
Typically we lint our javascript files using [Grunt](http://gruntjs.com/). We have two javascript linters with different features. [jshint](https://github.com/gruntjs/grunt-contrib-jshint) and [jscs](https://github.com/gustavohenke/grunt-jscs-checker).
9
+
Typically we lint our javascript files using [Grunt](http://gruntjs.com/). We have two javascript linters with different features. [jshint](https://github.com/gruntjs/grunt-contrib-jshint) and [jscs](https://github.com/gustavohenke/grunt-jscs).
Copy file name to clipboardExpand all lines: javascript/README.md
+45-23Lines changed: 45 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,5 @@
1
1
# General
2
-
##Comments
2
+
##Comments
3
3
4
4
Please comment code extensively. More comments are always better than fewer comments. Comments should describe the why of your code - not what the code is doing:
5
5
@@ -39,7 +39,7 @@ Don't commit commented out sections of code back into the repository. Just delet
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!
if ($('.blah').length=== defaultRoomCount) { ... }
67
67
````
68
68
69
-
Declare variables with var
69
+
##Declare variables with var
70
+
70
71
Separate declarations with a semicolon and a line break.
71
72
72
73
````javascript
@@ -87,20 +88,40 @@ var length = items.length;
87
88
var name ='foo';
88
89
````
89
90
90
-
Use a space before the opening curly brace
91
+
##Use function expressions over function declarations
92
+
93
+
The function expression is clearly recognisable as what it really is (a variable with a function value). Additionally, it helps organize code so that all variable declarations appear at the top of a file, and invocations follow. This gives some predictablity when others are reading your code, allowing for a more consistent structure.
94
+
95
+
Function expressions are subject to [hoisting](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting), and as such declaring functions as expressions will result in their being hoisted to the top of their enclosing function or global code.
96
+
97
+
98
+
````javascript
99
+
// bad
100
+
functionfoo () {
101
+
...
102
+
}
103
+
104
+
// good
105
+
varfoo=function() {
106
+
...
107
+
}
108
+
````
109
+
110
+
##Use a space before the opening curly brace for function calls
111
+
91
112
But no space before the opening argument parenthesis.
92
113
93
114
````javascript
94
115
// bad
95
-
functionfoo (){...}
116
+
varfoo=function(){...}
96
117
97
118
// good: whitespace improves readability here
98
-
functionfoo() {
119
+
varfoo=function() {
99
120
...
100
121
}
101
122
````
102
123
103
-
No preceding space before the opening parenthesis for function calls
124
+
##No preceding space before the opening parenthesis for function calls
104
125
105
126
````javascript
106
127
// bad: we want to differentiate between operations (see below) and function calls
@@ -110,7 +131,7 @@ doStuff ();
110
131
doStuff();
111
132
````
112
133
113
-
Cache the length in 'for' loops
134
+
##Cache the length in 'for' loops
114
135
115
136
````javascript
116
137
// bad: we are recalculating items.length every time
@@ -120,17 +141,17 @@ for (var i = 0; i < items.length; i++)
120
141
for (var i =0, length =items.length; i < length; i++)
121
142
````
122
143
123
-
Use a space before the opening parenthesis for operations
144
+
##Use a space before the opening parenthesis for conditions
124
145
125
146
````javascript
126
-
// bad: we want to differentiate between function calls (see above) and operations
147
+
// bad: we want to differentiate between function calls (see above) and conditions
127
148
if(true)
128
149
129
150
// good
130
151
if (true)
131
152
````
132
153
133
-
Use a space between operands
154
+
##Use a space between operands
134
155
135
156
````javascript
136
157
// bad
@@ -140,7 +161,7 @@ var c=a+b;
140
161
var c = a + b;
141
162
````
142
163
143
-
Don’t use a newline before 'else' or 'else if' statements
164
+
##Don’t use a newline before 'else' or 'else if' statements
144
165
145
166
````javascript
146
167
// bad
@@ -159,7 +180,7 @@ if (true) {
159
180
}
160
181
````
161
182
162
-
Use a space after colons
183
+
##Use a space after colons
163
184
164
185
````javascript
165
186
// bad
@@ -171,7 +192,7 @@ Use a space after colons
171
192
}
172
193
````
173
194
174
-
Use curly braces, even if the block only has one line
195
+
##Use curly braces, even if the block only has one line
175
196
176
197
````javascript
177
198
// bad: it's more work to add a line to this block
@@ -183,7 +204,7 @@ if (isVisible) {
183
204
}
184
205
````
185
206
186
-
Return early when possible
207
+
##Return early when possible
187
208
188
209
````javascript
189
210
if (condition) {
@@ -192,7 +213,8 @@ if (condition) {
192
213
// rest of codes
193
214
````
194
215
195
-
Don't use the object type in your variable name
216
+
##Don't use the object type in your variable name
217
+
196
218
Use a descriptive variable name instead.
197
219
198
220
````javascript
@@ -208,7 +230,7 @@ var items = [];
208
230
var computer = {};
209
231
````
210
232
211
-
Use camel-case for function names
233
+
##Use camelcase for function names
212
234
213
235
````javascript
214
236
// bad: underscores shouldn’t be used in identifiers
@@ -221,7 +243,7 @@ function RecalculateItemHeight() { ... }
221
243
functionrecalculateItemHeight() { ... }
222
244
````
223
245
224
-
Use camel-case for variable names
246
+
##Use camelcase for variable names
225
247
226
248
````javascript
227
249
// bad
@@ -231,7 +253,7 @@ var is_visible = true;
231
253
var isVisible =true;
232
254
````
233
255
234
-
Use Pascal case for constructors
256
+
##Use Pascal case for constructors
235
257
236
258
````javascript
237
259
// bad
@@ -249,7 +271,7 @@ function AwesomeMovie(options) {
249
271
var fiftyShades =newAwesomeMovie({ title:'50 Shades Of Grey' });
250
272
````
251
273
252
-
Declare methods for objects on the prototype, not in the object constructor
274
+
##Declare methods for objects on the prototype, not in the object constructor
0 commit comments