CoffeeScript
The main aim of this style-guide is to get the best possible readability (and not writability).
The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.
Naming Conventions
- Names SHOULD be descriptive
- camelCase MUST be used for naming objects, functions, and instances
- PascalCase MUST be used for classes and constructor names
Whitespace
- Tabs MUST be used for indentation
- It's RECOMMENDED to equal the indentation of a tab to 4 spaces
- You MAY use, however, a different number while working on the code to match your style preferences. Other common numbers of spaces are 2 and 8.
- There MUST be no trailing whitespace characters
- An empty newline MUST be placed at the end of a file
- Invisibles SHOULD be displayed during coding to reduce the likelihood of whitespace mistakes
- Tabs and spaces MUST NOT be mixed
Multi-line Statements
- Lines MUST not be longer than 80 characters (counting tabs as 4 spaces)
- When a statement is too long to fit on one line, line breaks MUST occur after an operator
Commas
-
Commas SHOULD be omitted were possible
foo =bar: 1baz: 2instead of
foo =bar: 1baz: 2and
fruits =applemelonpearinstead of
fruits =applemelonpear -
Inline commas MUST be followed by one space character
colors = 'green''yellow''red'instead of
colors = 'green''yellow''red'
Relational Operators
is
MUST be used over==
not
MUST be used over!
isnt
MUST be used over!=
or
MUST be used over||
and
MUST be used over&&
Comments
- There MUST be a space between
#
and the first character of the comment - The first character of the comment MUST be uppercase
- Single line comments MUST be placed on a newline above the subject of the comment
- One empty line SHOULD be put before a single line comment
- Obscure code SHOULD be commented but not obvious things
# FIXME:
MAY be used to annotate problems# TODO:
MAY be used to capture issues which need to get solved
Strings
-
Single Quotes MUST be used for simple strings except the string contains an interpolation (e.g.
"#{name}"
)test = 'foo'instead of
test = "foo"
Objects
-
Objects MUST be split up over several lines
vector =x: 10y: 20z: 30instead of
vector = x: 10y: 20z: 30 -
Shorthand assignments with braces MUST be used in single-line assignments
test = nameinstead of
test = name: name -
Objects with braces MUST be indented
returnx: 1y: 2instead of
returnx: 1y: 2
Functions
-
Parentheses for an empty argument list MUST be omitted
= ->consolelog 'test'instead of
=consolelog 'test' -
Fat arrows MUST only be used when necessary
= ->consolelog 'test'instead of
= =>consolelog 'test' -
Implicit returns SHOULD NOT be used
=return a + binstead of
= a + b -
Function calls with more than 2 arguments SHOULD be split over several lines
doSomething'with''those''strings'instead of
doSomething 'with''those''strings'
Chaining
-
Method chains with more than 2 method calls MUST be wrapped and indented.
$'#items'find'.selected'highlightinstead of
$'#items'find'.selected'highlight -
Sub-properties MUST not be wrapped
personaddresscountrytoUpperCasesplit''instead of
personaddresscountrytoUpperCasesplit'' -
Methods SHOULD return
@
to enable method chaining
Conditionals
-
Newlines SHOULD be insterted after
if
andelse
blocksif isTestdoSomethingdoSomethingElseelsedoNothingjumpAroundinstead of
if isTestdoSomethingdoSomethingElseelsedoNothingjumpAround
Type Casting
- To string:
String(123)
- To number:
Number('123')
- To boolean:
Boolean(1)
General
You MAY (when it's absolutely necessary) differ from any rules of this guide to increase the performance. You MUST, however, explain the reasons for not sticking to a rule in a comment.
Framework specific
jQuery
-
jQuery object variables MUST be prefixed with a
$
$form = $ '#myForm'instead of
form = $ '#myForm' -
Lookups MUST be cached
$form = $ '#form'$formcss'background-color': 'pink'# …$formhideinstead of
$'#form'css'background-color': 'pink'# …$'#form'hide