Skip to content

Comment on CoffeeScript: JavaScript without the Fail (slides)parent

Comments

I'm not going to fight on most of the non-parenthetical aspects of the syntax -- it always sounds like a holy war, everyone is right, and everyone is wrong.

I've not looked closely at the implementation, but Python concatenating adjacent strings might be a relic of C-based languages -- the same rule applies there and likely a standard of the language with CPython being the default underbelly of Python.

Then your final (YUI) example, we have "random" commas. Again, the language syntax starts to trend towards generally inconsistent, and I have to enforce more style guides upon my team.

NOTE: I work heavily with YUI as well, and my team -- Python stack -- too writes shims to support "kwargs"-like syntax, so I can't really consider coffeescript having an advantage. Quick pseudo comparison...

  YUI.module = function(name, func, kwargs){
    kwargs = kwargs || {};
    return YUI.add(name, function(Y){func(Y);}, kwargs.version, kwargs.details);
  };
  YUI.module("myCoolModule", function(Y){
    ...
  }, {"version": "1.0.0", "details": {"requires": ["app", "model"]});

The random comma in the yui example can be eliminated fairly easily. It being there is due to Coffeescript not being able to support the following call style when I initially wrote the shim.

    YUI.module = function(o, f) {
        var name = o.name,
            ver  = o.version;
        delete o.name;
        delete o.version;
        if(!name || !version) throw "Both `name` and `version` are required.";
        return YUI.add(name, f, ver, o);
    }

    YUI.module
      name: 'myCoolModule'
      version: 1.0
      requires: ['app','model']
      (Y) -> ...
Ultimately CoffeeScript is syntax sugar for Javascript that you pay for with wrong line numbers when debugging. I think it's worth it, but I have friends who disagree. My coffeescript source is about 30% fewer lines of code than the equivalent javascript. Roughly half of that is entirely closing braces/parens but the other half is actual code reduction/simplification, mostly from loop comprehensions.

I also use it for API design. My habit when doing the design is to write out code for how I'd like to use it. I always use coffeescript for this regardless of whether I'm implementing it in JS or CS because a bad api visually looks bad to me when I write it out and I bang at it until it looks right. I've been mocked for this process by a couple people who have asked for my input on their projects but I've never had someone not take my resulting recommendations.

> Roughly half of that is entirely closing braces/parens...

This is good, as it improves the code's readability.

> ...but the other half is actual code reduction/simplification, mostly from loop comprehensions.

This may not be good, as it can reduce the code's readability. List comprehension should only be used moderately, else you will end up with this:

http://blog.garlicsim.org/post/3504711416/nastiest-python-li...

Also, with python, at least you gain performance advantage by using list comprehension.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.