Hacker News new | ask | show | jobs
by mendelk 4226 days ago
I believe your example is a perfect demonstration of how coffeescript can cause unintended bugs.

You code compiles to:

  (function(req, res) {
    return getUserIds(req.query).then(function(ids) {
      return Promise.all(ids.map(function(id) {
        return getUser(id).then(function(user) {
          return user.friends.count;
        });
      }));
    }).then(function(response) {
      return res.send(200, response);
    })["catch"](function(error) {
      return res.send(500, error);
    });
  });
Not what intended, I assume :)
2 comments

I doubt anyone will see this, but just for posterity: Still not seeing a bug in this code, and under the tentative assumption that none exists, I find it amusing that you were confused by what you think is the more readable version :)
Well, that's definitely the JavaScript I intended to create :) Is there a bug in my logic somewhere?

Edit: But that aside, to be fair, I'll readily agree that it is easy to mess up CoffeeScript's significant whitespace if you don't pay attention to it. For example, you add a multi-line callback to one of those single-line callbacks at your peril!

But the tradeoff is, no mismatched braces/parenteses. Personally, I find the warts are not that hard to avoid, and the overall impact on my productivity is positive.

I guess he's referring to the implicit returns which are a pain in the neck to deal with.
Implicit returns are the norm in ES6's arrow functions. I use CoffeeScript for most of my personal coding, and I can think of exactly one situation where an implicit return got me in trouble. Generally, if you don't care enough about what your function returns to check it, then it's likely the calling function doesn't either, assuming you're familiar with the particular API you're working with.
In LiveScript there's syntax to explicitly omit any return value, it's just a function with !-> instead of -> for the arrow.
That's a question of perspective, I think-- if we're calling this function from somewhere, and we care about what it does, we need those returns.