My CoffeeScript style

Status: readable.

This page describes the way I like to write CoffeeScript.

Although this is my preferred style of writing, if I need to follow other standards, I have found EditorConfig to be useful.

Keep only what you need

I try to remove as many unnecessary (visible) characters as possible.

my_val = $('#my_input').val()

Can be written as:

my_val = $ '#my_input'
.val()

Although this only removes a pair of brackets and increases the vertical space, I find it easier to read.

Even further:

my_val = $ '#my_input'
.css 'border', 0
.data 'my_value', 12
.val()

This does remove my_val = from .val(), which can be confusing.

Blank lines

I have a single blank line between related statements and 2 blank lines between blocks of related statements.

a = my_function
    a: 1
    b: 2

b = 2

Since I prefer to write Literate CoffeeScript, I usually have 2 blank lines before each Markdown heading.

Brackets and commas

I use as few brackets and commas as possible. It seems less cluttered and avoids the blocks of closing brackets.

my_obj =
    a: 1
    b: 2

The exception to this is square brackets, because they are necessary to define an array.

my_list = [
    'a'
    1
    'b'
    2
]

Comments

Literate CoffeeScript lets me write my comments as Markdown paragraphs. This gives me the same character width (72) for all comments. It also allows headings and other HTML features.

I only use indented # comments for code that I want temporarily ignored.

Indentation

CoffeeScript uses indentation for scope and it is important to use the same type through a file.

I prefer using tabs instead of spaces.

Many people prefer to use 2 spaces for indentation, but I find this difficult to visually match up blocks of code that extend over many lines.

Spaces

I never include more than a single space between anything.

The only places I avoid spaces are when calling a function without parameters, or when assigning values in an object.

my_function()

my_obj = a: 1

I avoid trailing spaces because I have found that some editors remove them anyway.