Esa-Matti Suuronen

I make web gadgets

How to Write CoffeeScript Efficiently

| Comments

I have found few tricks that makes writing CoffeeScript more efficient and fun, especially when learning it and I’d like to share it with you.

These tricks are for Vim, but the ideas can be carried out to other editors as well. I know that at least the TextMate CoffeeScript Bundle can do some of these.

Basics

Let’s get the basics out of way. Get syntax hilighting from vim-coffee-script plugin and automatic syntax checking from Syntastic. These will take you a long way, but with CoffeeScript we can do more.

Reading compiled code

Especially when starting out with CoffeeScript you are not always sure what the snippet you are reading or even the code you just wrote does. Chances are that you already know Javascript so we can use that to our advantage. vim-coffee-script makes that incredibly easy.

Lets take following snippet that might be confusing to CoffeeScript newbies:

1
{@foo} = bar

With vim-coffee-script you can just select the snippet in Visual Mode and type :CoffeeCompile which will open up a new scratch buffer with a compiled version of the snippet which will clearly tell what this syntax in CoffeeScript means. You can use this to verify that you understood the CoffeeScript syntax by using your Javascript knowledge!

I recommend creating a shortcut for this. It’s so useful. Put this to your .vimrc:

1
2
vmap <leader>c <esc>:'<,'>:CoffeeCompile<CR>
map <leader>c :CoffeeCompile<CR>

This allows you to invoke the compiler with Leader-key + c. The leader key is backslash by default, but usually it is redefined to comma.

Stack Traces

I don’t like manually compiling CoffeeScript files for my Node.js apps. Instead I use the coffee command directly or use plain js wrapper app that starts my CoffeeScript apps. This is clean and simple, but can be painful when you get an exception. There is a stack trace, but it refers to the compiled Javacript file which does not exist! You could look up the original CoffeeScript file and try to guess what line the stack trace means by looking variable names or manually compile the file when exception occurs. Not so fun.

vim-coffee-script to the rescue!

When you execute the CoffeeCompile Vim command in Command Mode you will get the whole file compiled into the scratch buffer. In that you can scroll the line referred by the stack trace and see what code exactly rose it. This is bit clumsy since normally you can jump to a certain line by typing :<number>. We can do better! Put this to .vimrc:

1
command -nargs=1 C CoffeeCompile | :<args>

And then try typing :C<number>. Whoah! This takes you to the given line number in the compiled Javascript of the CoffeeScript file you are editing. Using it is just one character longer than normally jumping lines!

Python-like Decorators in CoffeeScript

| Comments

If you are not familar what decorators are in Python you should skim through this and this. In short they are a nice syntax for wrapping functions/methods with other functions in Python.

I really like decorators in Python and I sometimes miss them when working in other languages. Today at work it hit me when I was working on CoffeeScript project. It is really easy to implement Python-like decorators cleanly in CoffeeScript.

Decorators in Python

Here’s an example usage of Python decorator. Let’s pretend that this is a class for reading values from some device. It will give us values between 0 and 100, but in this app we want put a roof for the values it gives. We can create a decorator that limits the values given by the getter.

Decorator in Python (decorator_example.py) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python

# Our magic device
import random

def roof(amount):
    def decorator(method):
        def wrapper(*args):

            value = method(*args)
            if value > amount:
                return amount
            else:
                return value

        return wrapper
    return decorator


class Device(object):

    @roof(50)
    def get_value(self):
        # Read value from the device
        return random.randint(0, 100)


if __name__ == '__main__':
    reader = Device()
    for i in range(10):
        print reader.get_value()

Decorators in CoffeeScript

So that was an advanced configurable decorator for Python. Lets see how CoffeeScript handles the same situation.

Decorator in CoffeeScript [] (decorator_example.coffee) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env coffee

roof = (amount) -> (method) -> ->

  value = method.apply @, arguments

  if value > amount
    amount
  else
    value

class Device

    getValue: roof(50) ->
      parseInt Math.random() * 100


if require.main is module
  device = new Device
  for i in [0..10]
    console.log device.getValue()

Wow! That is a lot less syntax and no extra nesting!

This really shows how powerful anonymous functions and implicit returns are in CoffeeScript. Also the usage syntax would not be so clean if CoffeeScript didn’t have ability to call functions without the parenthesis.

The usage syntax is though better in Python, because you can stack decorators cleanly with it.

Stacking decorators in Python
1
2
3
4
5
class Device(object):
    @roof(50)
    @floor(10) # Checks the bottom of the value
    def get_value(self):
        return random.randint(0, 100)

In CoffeeScript must put them after each others which can get nasty if you have many decorators.

Piping decorators in CoffeeScript
1
2
3
class Device
    getValue: roof(50) floor(10) ->
      parseInt Math.random() * 100

But wait! There were no specific decorator syntax in Python in the old days. One could apply decorators just by calling it to the target and replacing the original method.

Oldschool decorator usage
1
2
3
4
5
6
class Device(object):
    def get_value(self):
        return random.randint(0, 100)

    get_value = roof(50)(get_value)
    get_value = floor(10)(get_value)

So you can do this in CoffeeScript

Piping decorators in CoffeeScript
1
2
3
4
5
6
class Device
  getValue: roof(50) ->
    parseInt Math.random() * 100

  getValue: roof(50) Device::getValue
  getValue: floor(10) Device::getValue

Pretty ugly, yeah, but might be better if you have tons of decorators.