Using coffeescript: combining stuff I love
Not to long ago I was introduced to Coffeescript through a node.js project. I was not immediately convinced but after having used it for a couple of months it grew on me. It feels like I can combine some stuff I really love: coffee, scripting and results.
Coffeescript is neat little language that sits on top of javascript. The compiler that comes with it transforms the coffeescript syntax into plain javascript. And this is also it's golden rule: "It's just JavaScript". This makes it usable in any situation you are or were using javascript before.
Javascript is really powerfull language with a strong object model. But there are some tricky things that come with javascript that do not always make it a pleasure to write. Coffeescript tries to take away the tricky bits and focus on the cool parts. Coffeescript makes it a lot less painfull to write a class with inheritance for instance:
class Monkey extends Animal
constructor: (legs) ->
super legs
# Make the monkey move
move: () ->
console.log "The monkey moves on #{@legs} legs"
Actually starting to use coffeescript is not that hard. In combination with a node.js and npm installation it's just a matter of npm install coffeescript and you are good to go. The executable that comes with the coffeescript lets you unleash it's awesomeness.
The coffeescript executable runs on node.js and when invoked with no parameters acts a wrapper around node.js. This has the advantage that you do not need to compile your coffescripts for use with node.js, the coffee executable will handle this for you.
The nice thing about coffeescript is that is does not interfere with normal node.js libraries. They can be used (with coffeescript syntax) within coffeescript with no alteration. The basic node.js server example looks like this in coffeescript:
http = require 'http'
server = http.createServer (request, response) ->
response.end 'Hello world'
server.listen 8080
Running this example requires using the coffee executable instead of the node executable:
mattijs@mattie: ~ coffee server.coffee # instead of node server.js
In the browser
Coffeescript is not only usable with node.js. Since it compiles to plain javascript it can also be used in the browser. This does involve the step of compiling your coffeescript to javascript. Luckilly the coffee executable can do this for you:
mattijs@mattie: ~ coffee -o output_directory -c script.coffee
The compiled javascript files can now be loaded into a webpage as normal javascript. Since compiled coffeescript is wrapped in closures exposing variables to the outside must be done by attaching them to the window object just like you would export variable in node.js with the exports variable.
There is also the option to have the browser compile the coffeescript. This is not recommended however. Coffeescript can be embeded into an HTML page with the text/coffeescript script type. With some glue you can enable the compiler (which must be included on the page) to parse all coffeescript and compile it to javascript.
Concluded
Coffeescript is a real breeze to write and can really speed up development. For small projects or pieces of code however it seems rather useless to write them in coffeescript. But when you're working on larger projects with several classes, libraries and people working on it, coffeescript can be real timesaver.
If you'd like to start using coffeescript I suggest taking a look at coffeescript.org. But don't try to take it on all at once.
Added bonus
Coffeescript comes with an added bonus if you use Mac OSX and Textmate. Because if you install the Coffeescript Textmate bundle you not only get syntax highlighting for Coffeescript (which is a must) but also the compiler that lets you compile scripts or snippets of Coffeescript from Textmate and see the result.