CoffeeScript
CoffeeScript is our preferred scripting language for the Rails conversion.* You should make an attempt use CoffeeScript, though some vendor-specific code may be difficult and confusing to integrate and Javascript may be easier and more readable. If you’re using Javascript, please leave a brief block-comment note at the top of the file with the reason for using JS. This will allow us to go back at a later time and understand why a decision was made.
Style
- Use
camelCasewith a leading lowercase for variables, methods, and object properties. - Use
CamelCasewith a leading uppercase for classes. - Use
SCREAMING_SNAKE_CASEfor constants. - Denote “private” methods and variables with a leading underscore.
- Prefer lines under 80 characters. Keep all lines under 100.
- Do not use trailing semicolons.
- Do not leave trailing whitespace.
Functions
- When declaring functions, use a single space after the closing parentheses of the argument list.
my_method = (arg1, arg2) -> # good
my_method = (arg1, arg2)-> # bad- When chaining method calls and the code doesn’t fit on one line, EVERY call should be on a separate line and indented one level:
[1..3]
.map((x) -> x * x)
.concat([10..12])
.filter((x) -> x < 11)- Choose to omit or include parentheses when calling functions in a way that optimizes readability.
Other
- Favor
unlessoveriffor negative conditions. - Do not use
elsewithunless. Rewrite toif..else. - Prefer string interpolation over concatenation.
*As time has gone on, this is not necessarily true. Consistency is key – use your best judgment. (Feb 2018)