Wednesday, September 14, 2011

First impressions of CoffeeScript

I first heard the word CoffeeScript banded about by a few people in the agile circles I run in a while ago. I heard that it was a language that compiled down to JavaScript but was more succinct and had better syntax. This instantly peeked my interest.

I've been coding Javascript for about 14 years and frankly I've always hated it. The advent of JQuery has made it more bearable, but it hasn't changed my feelings about JavaScript and don't get me started on JavaScript inheritance :)

I figured that anything that could improve on the syntax would be a good thing, so I decided to give it a shot.

Scott Hanselman recently posted about a plugin for VS.NET, called Mindscape Web Workbench, which allows realtime compilation of CoffeeScript to Javascript so I installed it and was off to the races.

Whenever I learn a new language I like to reimplement code I have written previous. This allows me to get the syntax down without worrying about logic. In this case I decided to rewrite a JQuery plugin, in our product at work, that I had worked on in the past.

I have to say that after some initial high hopes and early wins I'm rather disappointed.

I'm disappointed because the CoffeeScript syntax isn't too far removed from the way our Javascript is structured already and only slightly more succinct. We make heavy use of Knockout which is an MVVM framework. I'm not going to go into depth about Knockout, but if you want more information you can visit the Knockout website.

Here's an example of how our Javascript models are structured:

var exampleModel = {
    selection: ko.observable(""),
    text: ko.observable(""),
    isEverythingEntered : function () {
        return this.text().length > 0 && this.selection() > 0;
    }
};

Now here is that same code in CoffeeScript:

exampleModel:
    selection: ko.observable("")
    text: ko.observable("")
    isEverythingEntered ->
        this.text().length > 0 and this.selection() > 0

As you can see, they are very similar. This is a contrived example, but the more complex ones aren't any different.

The pros as I see them so far are as follows:
  • No more missing semicolons at the end of a line. Though Firefox and Chrome are robust when it comes to malformed JavaScript, IE isn't and will throw script errors if there's a missing semicolon's. Same goes for additional commas at the end of a block.
  • -> looks far nicer than having to write function() { }
The cons are as follows:
  • It relies on tabbed indenting. The rest of our codebase is space indented. At this time I'm not sure if this is a problem with CoffeeScript or with Mindscape Web Workbench.
  • I'm not a fan of the if then else syntax instead of a ternary operator.
  • Large Javascript files cause VS.NET to lag a bit. This isn't to do with CoffeeScript, but to do with the plugin.
Right now I'm not sure the pros are enough to adopt CoffeeScript.

I'm curious what your experiences are. Am I missing something?

No comments:

Post a Comment