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
Copy file name to clipboardExpand all lines: README.md
+85-30Lines changed: 85 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -79,9 +79,10 @@ UTF-8 is the preferred source file encoding.
79
79
80
80
If using a module system (CommonJS Modules, AMD, etc.), `require` statements should be placed on separate lines.
81
81
82
+
```coffeescript
82
83
require 'lib/setup'
83
84
Backbone = require 'backbone'
84
-
85
+
```
85
86
These statements should be grouped in the following order:
86
87
87
88
1. Standard library imports _(if a standard library exists)_
@@ -95,13 +96,17 @@ Avoid extraneous whitespace in the following situations:
95
96
96
97
- Immediately inside parentheses, brackets or braces
97
98
99
+
```coffeescript
98
100
($ 'body') # Yes
99
101
( $ 'body' ) # No
102
+
```
100
103
101
104
- Immediately before a comma
102
105
106
+
```coffeescript
103
107
console.log x, y # Yes
104
108
console.log x , y # No
109
+
```
105
110
106
111
Additional recommendations:
107
112
@@ -114,6 +119,7 @@ Additional recommendations:
114
119
115
120
-_(Do not use more than one space around these operators)_
116
121
122
+
```coffeescript
117
123
# Yes
118
124
x = 1
119
125
y = 1
@@ -123,11 +129,14 @@ Additional recommendations:
123
129
x = 1
124
130
y = 1
125
131
fooBar = 3
132
+
```
126
133
127
134
- Do not use spaces around the `=` sign when used to indicate a default parameter value
128
135
136
+
```coffeescript
129
137
test: (param=null) -> # Yes
130
138
test: (param = null) -> # No
139
+
```
131
140
132
141
<aname="comments"/>
133
142
## Comments
@@ -147,6 +156,7 @@ Each line of a block comment starts with a `#` and a single space, and should be
147
156
148
157
Paragraphs inside of block comments are separated by a line containing a single `#`.
149
158
159
+
```coffeescript
150
160
# This is a block comment. Note that if this were a real block
151
161
# comment, we would actually be describing the proceeding code.
152
162
#
@@ -157,6 +167,7 @@ Paragraphs inside of block comments are separated by a line containing a single
157
167
init()
158
168
start()
159
169
stop()
170
+
```
160
171
161
172
<aname="inline_comments"/>
162
173
### Inline Comments
@@ -169,13 +180,17 @@ The use of inline comments should be limited, because their existence is typical
169
180
170
181
Do not use inline comments when they state the obvious:
171
182
183
+
```coffeescript
172
184
# No
173
185
x = x + 1 # Increment x
186
+
```
174
187
175
188
However, inline comments can be useful in certain scenarios:
176
189
190
+
```coffeescript
177
191
# Yes
178
192
x = x + 1 # Compensate for border
193
+
```
179
194
180
195
<aname="naming_conventions"/>
181
196
## Naming Conventions
@@ -188,11 +203,15 @@ _(The **official** CoffeeScript convention is camelcase, because this simplifies
188
203
189
204
For constants, use all uppercase with underscores:
190
205
191
-
CONSTANT_LIKE_THIS
206
+
```coffeescript
207
+
CONSTANT_LIKE_THIS
208
+
```
192
209
193
210
Methods and variables that are intended to be "private" should begin with a leading underscore:
194
211
195
-
_privateMethod: ->
212
+
```coffeescript
213
+
_privateMethod: ->
214
+
```
196
215
197
216
<aname="functions"/>
198
217
## Functions
@@ -201,44 +220,58 @@ _(These guidelines also apply to the methods of a class.)_
201
220
202
221
When declaring a function that takes arguments, always use a single space after the closing parenthesis of the arguments list:
203
222
223
+
```coffeescript
204
224
foo = (arg1, arg2) -> # Yes
205
225
foo = (arg1, arg2)-> # No
226
+
```
206
227
207
228
Do not use parentheses when declaring functions that take no arguments:
208
229
230
+
```coffeescript
209
231
bar = -> # Yes
210
232
bar = () -> # No
233
+
```
211
234
212
235
In cases where method calls are being chained and the code does not fit on a single line, each call should be placed on a separate line and indented by one level (i.e., two spaces), with a leading `.`.
213
236
237
+
```coffeescript
214
238
[1..3]
215
239
.map((x) -> x * x)
216
240
.concat([10..12])
217
241
.filter((x) -> x < 11)
218
242
.reduce((x, y) -> x + y)
243
+
```
219
244
220
245
When calling functions, omit the parentheses on the final method call in a chain. For example:
221
246
247
+
```coffeescript
222
248
baz 12
223
249
224
250
foo(4).bar 8
251
+
```
225
252
226
253
You will sometimes see parentheses used to group functions (instead of being used to group function parameters). Examples of using this style (hereafter referred to as the "function grouping style"):
227
254
255
+
```coffeescript
228
256
($ '#selektor').addClass 'klass'
229
257
230
258
(foo 4).bar 8
259
+
```
231
260
232
261
This is in contrast to:
233
262
263
+
```coffeescript
234
264
$('#selektor').addClass 'klass'
235
265
236
266
foo(4).bar 8
267
+
```
237
268
238
269
The correct way to apply the function grouping style when chaining is to use it for the initial call only:
239
270
271
+
```coffeescript
240
272
($ '#selektor').addClass('klass').hide() # Yes
241
273
($ '#selektor').(addClass 'klass').hide() # No
274
+
```
242
275
243
276
This guide does not prescribe the use of the function grouping style or the alternative. However, **if the function grouping style is adopted for a particular project, be consistent with its usage.**
244
277
@@ -247,8 +280,10 @@ This guide does not prescribe the use of the function grouping style or the alte
247
280
248
281
Use string interpolation instead of string concatenation:
249
282
283
+
```coffeescript
250
284
"this is an #{adjective} string" # Yes
251
285
"this is an " + adjective + " string" # No
286
+
```
252
287
253
288
Prefer single quoted strings (`''`) instead of double quoted (`""`) strings, unless features like string interpolation are being used for the given string.
254
289
@@ -259,51 +294,61 @@ Favor `unless` over `if` for negative conditions.
259
294
260
295
Instead of using `unless...else`, use `if...else`:
261
296
262
-
# Yes
263
-
if true
264
-
...
265
-
else
266
-
...
297
+
```coffeescript
298
+
# Yes
299
+
if true
300
+
...
301
+
else
302
+
...
267
303
268
-
# No
269
-
unless false
270
-
...
271
-
else
272
-
...
304
+
# No
305
+
unless false
306
+
...
307
+
else
308
+
...
309
+
```
273
310
274
311
Multi-line if/else clauses should use indentation:
275
312
276
-
# Yes
277
-
if true
278
-
...
279
-
else
280
-
...
313
+
```coffeescript
314
+
# Yes
315
+
if true
316
+
...
317
+
else
318
+
...
281
319
282
-
# No
283
-
if true then ...
284
-
else ...
320
+
# No
321
+
if true then ...
322
+
else ...
323
+
```
285
324
286
325
<aname="looping_and_comprehensions"/>
287
326
## Looping and Comprehensions
288
327
289
328
Take advantage of comprehensions whenever possible:
290
329
330
+
```coffeescript
291
331
# Yes
292
332
result = (item.name for item in array)
293
333
294
334
# No
295
335
results = []
296
336
for item in array
297
337
results.push item.name
338
+
```
298
339
299
340
To filter:
300
341
301
-
result = (item for item in array when item.name is "test")
342
+
```coffeescript
343
+
result = (item for item in array when item.name is "test")
344
+
```
302
345
303
346
To iterate over the keys and values of objects:
304
347
305
-
object = one: 1, two: 2
306
-
alert("#{key} = #{value}") for key, value of object
348
+
```coffeescript
349
+
object = one: 1, two: 2
350
+
alert("#{key} = #{value}") for key, value of object
351
+
```
307
352
308
353
<aname="#extending_native_objects"/>
309
354
## Extending Native Objects
@@ -326,15 +371,19 @@ Write the annotation on the line immediately above the code that the annotation
326
371
327
372
The annotation keyword should be followed by a colon and a space, and a descriptive note.
328
373
374
+
```coffeescript
329
375
# FIXME: The client's current state should *not* affect payload processing.
330
376
resetClientState()
331
377
processPayload()
378
+
```
332
379
333
380
If multiple lines are required by the description, indent subsequent lines with two spaces:
334
381
382
+
```coffeescript
335
383
# TODO: Ensure that the value returned by this call falls within a certain
336
384
# range, or throw an exception.
337
385
analyze()
386
+
```
338
387
339
388
Annotation types:
340
389
@@ -359,23 +408,29 @@ If a custom annotation is required, the annotation should be documented in the p
359
408
360
409
`or=` should be used when possible:
361
410
362
-
temp or= {} # Yes
363
-
temp = temp || {} # No
411
+
```coffeescript
412
+
temp or= {} # Yes
413
+
temp = temp || {} # No
414
+
```
364
415
365
416
Prefer shorthand notation (`::`) for accessing an object's prototype:
366
417
367
-
Array::slice # Yes
368
-
Array.prototype.slice # No
418
+
```coffeescript
419
+
Array::slice # Yes
420
+
Array.prototype.slice # No
421
+
```coffeescript
369
422
370
423
Prefer `@property` over `this.property`.
371
424
372
425
Avoid `return` where not required, unless the explicit return increases clarity.
373
426
374
427
Use splats (`...`) when working with functions that accept variable numbers of arguments:
0 commit comments