Conclusions on CoffeeScript

Well, having played with CoffeeScript for a few days, I think it’s a wonderful thing – but for the moment, at least, I’m going back to JavaScript, with some regret.

It’s a fabulous, clean, simple language – I love the terseness of the code:

class TagPanel
  constructor: (width, height, paper) ->
    @width: width
    @height: height
    @paper: paper
    @boxes: []

  add_box: (box) ->
    box.display(@paper)
    @boxes.push box

  animate: (count) ->
    for i in [1...count]
      for box in @boxes
        box.step()

this.TagPanel: TagPanel # make global

compared with the JavaScript output:

(function(){
  var TagPanel;
  TagPanel = function TagPanel(width, height, paper) {
    this.width = width;
    this.height = height;
    this.paper = paper;
    this.boxes = [];
    return this;
  };
  TagPanel.prototype.add_box = function add_box(box) {
    box.display(this.paper);
    return this.boxes.push(box);
  };
  TagPanel.prototype.animate = function animate(count) {
    var _a, _b, _c, _d, _e, _f, _g, _h, _i, box, i;
    _a = []; _d = 1; _e = count;
    for (_c = 0, i = _d; (_d <= _e ? i  _e); (_d <= _e ? i += 1 : i -= 1), _c++) {
      _a.push((function() {
        _f = []; _g = this.boxes;
        for (_h = 0, _i = _g.length; _h <; _i; _h++) {
          box = _g[_h];
          _f.push(box.step());
        }
        return _f;
      }).call(this));
    }
    return _a;
  };
  this.TagPanel = TagPanel;
  // make global
})();

However, I think it fails (for me) for a few reasons; some of these may be resolved over time, some may not.
I think the main problem is that it's too different to Javascript. This is the same reason I'm not a fan of HAML or SASS, but quite like LessCSS – if you are too different to the core language, a number of things no longer work:

  • Syntax highlighting and IDE support are minimal
  • You aren’t learning enough about the core language
  • You can’t easily use existing code snippets or examples, with mentally de-compiling them back to CoffeeScript

LessCSS, or in-language templating tools like Mustache or even good ol’ ERB, let you mix the old and the new; sometimes you IDE still gets screwed up by the differences from the core language, but you can generally work it out.

I have a few other minor issues with CofeeScript – I’m not such a big fan of Python-style indentation-as-code; I used to be more of a fan, and I could learn to love it again, but without IDE support it’s a bit of a pain. Also there are a few other things I’d quibble about; but they are just that, quibbles, most of it is just fine as a language.

But a key thing for me is that I’m still learning to do Javascript properly – after years of using it badly as a make-my-browser-clicks-work tool, I went to a talk by Doug Crockford, and realised I need to learn this language properly. And that means becoming familiar with the language itself, learning the right way and the wrong way to do things; using CoffeeScript encourages a few particular idioms, which work well, but I’m not learning beyond those idioms.

So I’ll take my code spiked in CoffeeScript and try to do it properly in JavaScript, probably working on TDDing it with JSpec or other such tools. Maybe when I’m completely confident in JavaScript I’ll come back to the sweet smell of the Coffee…



Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image