Skip to content

Instantly share code, notes, and snippets.

@hemanth
Forked from gerardpaapu/gist:3118854
Created July 16, 2012 04:52
Show Gist options
  • Save hemanth/3120635 to your computer and use it in GitHub Desktop.
Save hemanth/3120635 to your computer and use it in GitHub Desktop.

I love Coffeescript, but write Javascript

This is an example of why even though I love coffeescript, I still frequently write code in raw javascript.

This is the coffeescript I originally had:

pairs = []

for key, value of @data  
    pairs.push( _(key) + '=' + _(value) )

And this is the javascript it output

var pairs = [], value, _ref1;

_ref1 = this.data;
for (key in _ref1) {
    value = _ref1[key];
    pairs.push(_(key) + '=' + _(value))
}

I rewrote the coffeescript to be more succint and idiomatic(?)

pairs = for key, value of @data  
    _(key) + '=' + _(value)

And of course I end up with this code:

var pairs = (function () {
    var _ref1, _results;
    _ref1 = this.data;
    _results = [];

    for (key in _ref1) {
        value = _ref1[key];
        _results.push(_(key) + '=' + _(value));
    }
    return _results;
}());

Nothing in the semantics has changed (in my head), but to get the more performant code I have to write uglier more explicit coffeescript. I hope that in the near future a compiler would do this for me (perhaps closure already can?).

The more I have to think about what the javascript output is going to be, the more I would rather write in javascript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment