This guide presents a collection of best-practices and coding conventions for the CoffeeScript programming language.
This guide is intended to be community-driven, and contributions are highly encouraged.
Please note that this is a work-in-progress: there is much more that can be specified, and some of the guidelines that have been specified may not be deemed to be idiomatic by the community (in which case, these offending guidelines will be modified or removed, as appropriate).
The details in this guide have been very heavily inspired by several existing style guides and other resources. In particular:
This repository can be installed and used to lint coffescript files using the configuration found in the coffeelint config file. List this repository as a dependency in the package.json
and add the following to your Makefile:
Files must either be specified by their absolute path, or their path relative to the Makefile's directory. If no files are specified and you are in a git repository all .coffee
files in that repository will be linted. If you are not in a git repository all .coffee
files in the current directory and all subdirectories (except node_modules
) will be linted.
### Block Comments
Block comments apply to the block of code that follows them.
Each line of a block comment starts with a #
and a single space, and should be indented at the same level of the code that it describes.
Paragraphs inside of block comments are separated by a line containing a single #
.
# This is a block comment. Note that if this were a real block
# comment, we would actually be describing the proceeding code.
#
# This is the second paragraph of the same block comment. Note
# that this paragraph was separated from the previous paragraph
# by a line containing a single comment character.
init()
start()
stop()
### Inline Comments
Inline comments are placed on the line immediately above the statement that they are describing. If the inline comment is sufficiently short, it can be placed on the same line as the statement (separated by a single space from the end of the statement).
All inline comments should start with a #
and a single space.
The use of inline comments should be limited, because their existence is typically a sign of a code smell.
Do not use inline comments when they state the obvious:
# No
x = x + 1 # Increment x
However, inline comments can be useful in certain scenarios:
# Yes
x = x + 1 # Compensate for border
Methods and variables that are intended to be "private" should begin with a leading underscore:
## Functions
(These guidelines also apply to the methods of a class.)
When declaring a function that takes arguments, always use a single space after the closing parenthesis of the arguments list:
foo = (arg1, arg2) -> # Yes
foo = (arg1, arg2)-> # No
Do not use parentheses when declaring functions that take no arguments:
bar = -> # Yes
bar = () -> # No
Only use the fat arrow syntax when this
is needed within the function body:
baz = => 1 # No
baz = => @bang # Yes
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 .
.
[1..3]
.map((x) -> x * x)
.concat([10..12])
.filter((x) -> x < 11)
.reduce((x, y) -> x + y)
When calling functions, choose to omit or include parentheses in such a way that optimizes for readability. Keeping in mind that "readability" can be subjective, the following examples demonstrate cases where parentheses have been omitted or included in a manner that the community deems to be optimal:
baz 12
brush.ellipse x: 10, y: 20 # Braces can also be omitted or included for readability
foo(4).bar(8)
obj.value(10, 20) / obj.value(20, 10)
print inspect value
new Tag(new Value(a, b), new Arg(c))
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"):
($ '#selektor').addClass 'klass'
(foo 4).bar 8
This is in contrast to:
$('#selektor').addClass 'klass'
foo(4).bar 8
In cases where method calls are being chained, some adopters of this style prefer to use function grouping for the initial call only:
($ '#selektor').addClass('klass').hide() # Initial call only
(($ '#selektor').addClass 'klass').hide() # All calls
The function grouping style is not recommended. However, if the function grouping style is adopted for a particular project, be consistent with its usage.
Don't assign default arguments in front of positional arguments:
bad = (foo='bar', baz) -> # No
bad = (foo, bar='baz') -> # Yes
## Annotations
Use annotations when necessary to describe a specific action that must be taken against the indicated block of code.
Write the annotation on the line immediately above the code that the annotation is describing.
The annotation keyword should be followed by a colon and a space, and a descriptive note.
# FIXME: The client's current state should *not* affect payload processing.
resetClientState()
processPayload()
If multiple lines are required by the description, indent subsequent lines with two spaces:
# TODO: Ensure that the value returned by this call falls within a certain
# range, or throw an exception.
analyze()
Annotation types:
TODO
: describe missing functionality that should be added at a later dateFIXME
: describe broken code that must be fixedOPTIMIZE
: describe code that is inefficient and may become a bottleneckHACK
: describe the use of a questionable (or ingenious) coding practiceREVIEW
: describe code that should be reviewed to confirm implementation
If a custom annotation is required, the annotation should be documented in the project's README.
## Miscellaneous
and
is preferred over &&
.
or
is preferred over ||
.
is
is preferred over ==
.
not
is preferred over !
.
or=
should be used when possible:
temp or= {} # Yes
temp = temp || {} # No
Prefer shorthand notation (::
) for accessing an object's prototype:
Array::slice # Yes
Array.prototype.slice # No
Prefer @
over this
.
return @property # Yes
return this.property # No
return @ # Yes
return this # No
Avoid return
where not required, unless the explicit return increases clarity.
Use splats (...
) when working with functions that accept variable numbers of arguments:
console.log args... # Yes
(a, b, c, rest...) -> # Yes