CoffeeScript – Bring back the joy to JavaScript

Some time agoe, I was again (as many times before often) in a snit about JavaScripts verbosity and gave a try to CoffeeScript that amazed me within the first minute of usage. CoffeeScript is a small language on top of JavaScript, that before execution in the Browser/on the Server has to be compiled down to JavaScript syntax. It removes most of JavaScripts verbosity and provides clear access to JavaScripts gorgeous Object-Model with a purist syntax.

The fact that JavaScript was created during the browser wars by two rivaling companies (NS & M$) made the concept of the language, that in is heart is really fantastic, somewhat quirky to use. Many smart developers already worked on this problem. Douglas Crockford for example invented “The good parts” of JavaScript (an article and the video can be found on the YUI Blog) and many good libraries like Prototype and jQuery where invented to deal with the “Bad parts” of JavaScript.

With these libraries we at least have a clean and browser neutral API to program JavaScript that was a huge step. In my opinion this made it possible at all for the Web 2.0 to rise and JavaScript to get so popular.

As the API could be enhanced by these libraries, they could not change the languages Syntax. This step is taken by CoffeeScript that I see as the next step in the JS evolution. It completely removes semicolons and curly braces. Based on structure by indentation the syntax looks similar to Python. Also it provides many neat shortcuts for common tasks that are very verbose and complex in normal JavaScript. Everybody knows the problem of loosing the this context of a function when passing as a callback.

So here is a first example of how CoffeeScript is way more compact and readable that JavaScript because it removes a lot of noise from the code. So here is a small jQuery based JavaScript snipped.

And now I converted the exact same snipped to CoffeeScript. See for yourself how awesome it is.

CoffeeScript integrates very nicely with jQuery and other JavaScript libraries and it does not matter if they are written in CoffeeScript itself or JavaScript. Remember, CoffeeScript gets compiled to JavaScript before execution.

The CoffeeScript compiler itself is written in CoffeeScript and can be install through the node package manager npm. It can not only be used in the browser but also to create fancy nodejs programs that run server side and take away some of the pain from the callback hell that comes with asynchronous programming.

So if your not already convinced, you should give it a try and be already amazed after minutes of using CoffeeScript.

 

Leave a Reply

Your email address will not be published. Required fields are marked *