Skip to content

Commit a8ebf61

Browse files
committed
syntax highlighting
1 parent bfcdffb commit a8ebf61

File tree

1 file changed

+85
-30
lines changed

1 file changed

+85
-30
lines changed

README.md

Lines changed: 85 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,10 @@ UTF-8 is the preferred source file encoding.
7979

8080
If using a module system (CommonJS Modules, AMD, etc.), `require` statements should be placed on separate lines.
8181

82+
```coffeescript
8283
require 'lib/setup'
8384
Backbone = require 'backbone'
84-
85+
```
8586
These statements should be grouped in the following order:
8687

8788
1. Standard library imports _(if a standard library exists)_
@@ -95,13 +96,17 @@ Avoid extraneous whitespace in the following situations:
9596

9697
- Immediately inside parentheses, brackets or braces
9798

99+
```coffeescript
98100
($ 'body') # Yes
99101
( $ 'body' ) # No
102+
```
100103

101104
- Immediately before a comma
102105

106+
```coffeescript
103107
console.log x, y # Yes
104108
console.log x , y # No
109+
```
105110

106111
Additional recommendations:
107112

@@ -114,6 +119,7 @@ Additional recommendations:
114119

115120
- _(Do not use more than one space around these operators)_
116121

122+
```coffeescript
117123
# Yes
118124
x = 1
119125
y = 1
@@ -123,11 +129,14 @@ Additional recommendations:
123129
x = 1
124130
y = 1
125131
fooBar = 3
132+
```
126133

127134
- Do not use spaces around the `=` sign when used to indicate a default parameter value
128135

136+
```coffeescript
129137
test: (param=null) -> # Yes
130138
test: (param = null) -> # No
139+
```
131140

132141
<a name="comments"/>
133142
## Comments
@@ -147,6 +156,7 @@ Each line of a block comment starts with a `#` and a single space, and should be
147156

148157
Paragraphs inside of block comments are separated by a line containing a single `#`.
149158

159+
```coffeescript
150160
# This is a block comment. Note that if this were a real block
151161
# comment, we would actually be describing the proceeding code.
152162
#
@@ -157,6 +167,7 @@ Paragraphs inside of block comments are separated by a line containing a single
157167
init()
158168
start()
159169
stop()
170+
```
160171

161172
<a name="inline_comments"/>
162173
### Inline Comments
@@ -169,13 +180,17 @@ The use of inline comments should be limited, because their existence is typical
169180

170181
Do not use inline comments when they state the obvious:
171182

183+
```coffeescript
172184
# No
173185
x = x + 1 # Increment x
186+
```
174187

175188
However, inline comments can be useful in certain scenarios:
176189

190+
```coffeescript
177191
# Yes
178192
x = x + 1 # Compensate for border
193+
```
179194

180195
<a name="naming_conventions"/>
181196
## Naming Conventions
@@ -188,11 +203,15 @@ _(The **official** CoffeeScript convention is camelcase, because this simplifies
188203

189204
For constants, use all uppercase with underscores:
190205

191-
CONSTANT_LIKE_THIS
206+
```coffeescript
207+
CONSTANT_LIKE_THIS
208+
```
192209

193210
Methods and variables that are intended to be "private" should begin with a leading underscore:
194211

195-
_privateMethod: ->
212+
```coffeescript
213+
_privateMethod: ->
214+
```
196215

197216
<a name="functions"/>
198217
## Functions
@@ -201,44 +220,58 @@ _(These guidelines also apply to the methods of a class.)_
201220

202221
When declaring a function that takes arguments, always use a single space after the closing parenthesis of the arguments list:
203222

223+
```coffeescript
204224
foo = (arg1, arg2) -> # Yes
205225
foo = (arg1, arg2)-> # No
226+
```
206227

207228
Do not use parentheses when declaring functions that take no arguments:
208229

230+
```coffeescript
209231
bar = -> # Yes
210232
bar = () -> # No
233+
```
211234

212235
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 `.`.
213236

237+
```coffeescript
214238
[1..3]
215239
.map((x) -> x * x)
216240
.concat([10..12])
217241
.filter((x) -> x < 11)
218242
.reduce((x, y) -> x + y)
243+
```
219244

220245
When calling functions, omit the parentheses on the final method call in a chain. For example:
221246

247+
```coffeescript
222248
baz 12
223249

224250
foo(4).bar 8
251+
```
225252

226253
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"):
227254

255+
```coffeescript
228256
($ '#selektor').addClass 'klass'
229257

230258
(foo 4).bar 8
259+
```
231260

232261
This is in contrast to:
233262

263+
```coffeescript
234264
$('#selektor').addClass 'klass'
235265

236266
foo(4).bar 8
267+
```
237268

238269
The correct way to apply the function grouping style when chaining is to use it for the initial call only:
239270

271+
```coffeescript
240272
($ '#selektor').addClass('klass').hide() # Yes
241273
($ '#selektor').(addClass 'klass').hide() # No
274+
```
242275

243276
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.**
244277

@@ -247,8 +280,10 @@ This guide does not prescribe the use of the function grouping style or the alte
247280

248281
Use string interpolation instead of string concatenation:
249282

283+
```coffeescript
250284
"this is an #{adjective} string" # Yes
251285
"this is an " + adjective + " string" # No
286+
```
252287

253288
Prefer single quoted strings (`''`) instead of double quoted (`""`) strings, unless features like string interpolation are being used for the given string.
254289

@@ -259,51 +294,61 @@ Favor `unless` over `if` for negative conditions.
259294

260295
Instead of using `unless...else`, use `if...else`:
261296

262-
# Yes
263-
if true
264-
...
265-
else
266-
...
297+
```coffeescript
298+
# Yes
299+
if true
300+
...
301+
else
302+
...
267303

268-
# No
269-
unless false
270-
...
271-
else
272-
...
304+
# No
305+
unless false
306+
...
307+
else
308+
...
309+
```
273310

274311
Multi-line if/else clauses should use indentation:
275312

276-
# Yes
277-
if true
278-
...
279-
else
280-
...
313+
```coffeescript
314+
# Yes
315+
if true
316+
...
317+
else
318+
...
281319

282-
# No
283-
if true then ...
284-
else ...
320+
# No
321+
if true then ...
322+
else ...
323+
```
285324

286325
<a name="looping_and_comprehensions"/>
287326
## Looping and Comprehensions
288327

289328
Take advantage of comprehensions whenever possible:
290329

330+
```coffeescript
291331
# Yes
292332
result = (item.name for item in array)
293333

294334
# No
295335
results = []
296336
for item in array
297337
results.push item.name
338+
```
298339

299340
To filter:
300341

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+
```
302345

303346
To iterate over the keys and values of objects:
304347

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+
```
307352

308353
<a name="#extending_native_objects"/>
309354
## Extending Native Objects
@@ -326,15 +371,19 @@ Write the annotation on the line immediately above the code that the annotation
326371

327372
The annotation keyword should be followed by a colon and a space, and a descriptive note.
328373

374+
```coffeescript
329375
# FIXME: The client's current state should *not* affect payload processing.
330376
resetClientState()
331377
processPayload()
378+
```
332379

333380
If multiple lines are required by the description, indent subsequent lines with two spaces:
334381

382+
```coffeescript
335383
# TODO: Ensure that the value returned by this call falls within a certain
336384
# range, or throw an exception.
337385
analyze()
386+
```
338387

339388
Annotation types:
340389

@@ -359,23 +408,29 @@ If a custom annotation is required, the annotation should be documented in the p
359408

360409
`or=` should be used when possible:
361410

362-
temp or= {} # Yes
363-
temp = temp || {} # No
411+
```coffeescript
412+
temp or= {} # Yes
413+
temp = temp || {} # No
414+
```
364415

365416
Prefer shorthand notation (`::`) for accessing an object's prototype:
366417

367-
Array::slice # Yes
368-
Array.prototype.slice # No
418+
```coffeescript
419+
Array::slice # Yes
420+
Array.prototype.slice # No
421+
```coffeescript
369422

370423
Prefer `@property` over `this.property`.
371424

372425
Avoid `return` where not required, unless the explicit return increases clarity.
373426

374427
Use splats (`...`) when working with functions that accept variable numbers of arguments:
375428

376-
console.log args... # Yes
429+
```coffeescript
430+
console.log args... # Yes
377431

378-
(a, b, c, rest...) -> # Yes
432+
(a, b, c, rest...) -> # Yes
433+
```
379434

380435
[coffeescript]: http://jashkenas.github.com/coffee-script/
381436
[coffeescript-issue-425]: https://github.com/jashkenas/coffee-script/issues/425

0 commit comments

Comments
 (0)