Latest posts

CoffeeScript – emphasising the good parts

This article is a very cursory introduction to CoffeeScript.

I’d forgive you if you told me that you don’t like writing Javascript, after all it has a few bad parts and a few more really, really bad parts, but there are a couple of things you have to acknowledge:

  • It’s a good language at heart
  • It’s everywhere

Even if I can’t sell you on 1, 2 is irrefutable. This means, like it or not, you and Javascript are stuck with one another right?

Well, not really.

CoffeeScript is a neat little language which cross-compiles to Javascript. It emphasises the good parts of the language and it uses a syntax inspired by Python and Ruby aimed at improving the brevity and readability of the code you write. Quicker development and more maintainable code you say? Hell yes!

My first cup

One particularly slow evening I was sat at home, alone, on Github (ladies?) when I came across Computer Science in Javascript by Nicholas C. Zakas. When I saw the Base64 encoding/decoding example (writeup) I let out a nerdy squeal ? “EEEE, this would be even cooler in CoffeeScript, I could convert it!”.

So I did. I then embedded it all in Nicholas’s original test document using “text/coffeescript” tags. Neat.

Although the rendered page is far from pretty, you’ll see that the interpreted CoffeeScript passes all the tests written for the JavaScript version, and that the source is clear, concise and readable.

The tasty bits

  • Less syntactic clutter than JavaScript
  • Once you learn the ropes, you’ll probably find it faster to write than JavaScript.
  • Significant whitespace (Yeah, you heard me - I like it)
  • It doesn’t lock you in ? the JS it produces is pretty damn good. Even JSLint approves!
  • It plays nicely with existing JS libraries
  • It hit 1.0 last December
  • If you’re an adventurous (read geeky) type, you can use it with node.js too
  • Its name is ripe for parody.

The not-so-tasty bits

  • If you’re not automating your deployments, compilation adds an extra step when you’re publishing your site/app
  • There is some learning curve if if you’ve never written any Python or Ruby before
  • You’ll need to know a bit of Javascript before you get started with CoffeeScript (this is mostly for debugging)
  • As Jeremy Ashkenas says: CoffeeScript is for sissies whereas Javascript is for real cowboys

Yeah, but is anybody actually using it?

CoffeeScript is already being embraced by a number of forward thinking developers and tech companies. A few notable examples being:

Conclusion

CoffeeScript is clear, concise, powerful and most of all it’s fun. Give it a shot next time you need to write some JS - you’ll like it, promise.

Getting Started Resources

Erskine Design
Published in: