

A Python programmer’s first impression of CoffeeScript - DanielRibeiro
http://blog.ssokolow.com/archives/2011/05/07/a-python-programmers-first-impression-of-coffeescript/

======
shazow
My biggest complaint about CoffeeScript is lines like these:

    
    
      $('.shopping_cart').bind 'click', (event) =>
        @customer.purchase @cart
    

Perhaps it's just me but it feels like there's no rhyme or pattern to the use
of punctuation and spaces. This is how my brain parses it:

    
    
      variable(string).variable string, (variable) =>
        @variable.variable @variable
    

Specifically it doesn't feel obvious that 'click' is a parameter of bind, and
event is a parameter of a new anonymous function, and @cart is being called
with @customer.purchase. If I stare at it enough, I can figure it out, but
it's not obvious.

Compared to normal JavaScript...

    
    
      var self = this;
      $('.shopping_card').bind('click', function(event) {
        return self.customer.purchase(self.cart);
      });
    

If you're using a framework, you can even get rid of the 'self' bit with a
wrapper that binds the 'this' context.

In JavaScript, where there are parenthesis, you know that something is being
called. In the CoffeScript example, there are 4 calls:

    
    
      $('.shopping_card')
      bind 'click'
      , (event) /* (Update: This is part of a function definition, my bad.) */
      @customer.purchase @cart
    

Two are called with parenthesis, two are called with spaced positional
arguments. Why?

If I understand correctly, parenthesis for calls are generally optional, but I
would argue this is a bad thing. The inconsistent use prevents me from being
able to skim code and get a feel for what it's doing. Also not to mention the
optional commas in lists:

    
    
      contenders = [
        "Michael Phelps"
        "Liu Xiang"
        "Yao Ming"
      ]
    

I see more good reasons to keep the commas (improves flow, explicit) than to
get rid of them (I suppose readers could still figure it out if they tried and
it's one less character).

I am a fan of the space-based blocks and single-line lambdas like square = (x)
-> x * x

Instead of Python meets Haskell, it feels closer to a mix between Ruby's
ambiguous syntax and YAML's indent/line-based parsing. If anyone else ran into
these "issues" and but grew to appreciate them, I'd be interested in hearing
about it.

~~~
cheald
Coffeescript does inherit its parentheses-are-optional attitude from Ruby, and
as a Ruby programmer, it makes perfect sense to me. That said, just because
they're optional doesn't mean it's required to be omitted; I'll often use
parentheses when I feel that adding them improves code clarity.

Having been writing Ruby for a few years, I really dig that parentheses aren't
required - you can write code that reads a lot more like English and a lot
less like...well, code. It does take some mental re-training though. Once
you've grown accustomed to it, it scans really nicely, and while it's very
possible to write code that's difficult to visually parse, good programmers
will use parentheses when they improve clarity, and omit them when they don't.

It helps a LOT to remember that in Coffeescript, you're more or less never
naming functions - you're just passing around a bunch of lambdas. The function
syntax tripped me up at first, as well, but once I got a handle on what it was
doing, it makes a lot of sense.

I've described Coffeescript as "Javascript in drag as Ruby with a Python under
her skirt", and I think it's a fairly apt description. :P

~~~
jashkenas
I think this a great attitude towards optional syntax. For example:

    
    
       xCoord(points[0]) - xCoord(points[1])
    

...is clear, but paren-ful, whereas:

    
    
       print object
    

...is also clear, but has no need of parens.

------
ivanzhao
I tried CoffeeScript for a project, and went back to plain Javascript.

Maybe that's a Ruby/Perl thing that I didn't get. It's almost "too
expressive", which I found myself spent quite some time thinking about "how to
say something" in a elegant manner, instead of just saying it.

~~~
riobard
Just say it first and then worry about how to say it more elegantly. I know
it's hard to resist the urge to make it perfect the first time, but I guess
that's required skill in many situations anyway.

------
Tycho
I rewrote the JS of a webapp in javascript today, enjoyed it a lot. The only
thing I'm puzzled about is how you do ajax callbacks (with jQuery). Like how
you would add an anonymous callback function. But it makes a good excuse for
refactoring messy javascript, and it's not hard to pick up. I'd say the
readability improvement is instant and applies to readers who've never tried
Coffeescript, even if they know JS.

~~~
jashkenas

        $.getJSON url, (response) -> ...
    
        # Or, more detailed...
    
        $.ajax
          url: "/query.json"
          type: "GET"
          success: (response) -> 
            process response
          error: -> 
            alert "trouble!"

------
thegoleffect
For "Switch statements don’t allow you to fall-through to the next case", you
can combine multiple cases:

when "a", "b" then doStuff()

------
bballbackus
I don't like that Coffeescript requires a compiler to create the actual
javascript, but that they don't offer a cross-platform solution for this
compiler. Yes, you can get the compiler working on Windows with Cygwin, but a
better solution would be a windows compatible compiler that didn't make me go
through the hassle of setting up Cygwin in the process.

~~~
vindvaki
You don't need Cygwin to "install" CoffeeScript. You can even run it in your
browser:

<http://jashkenas.github.com/coffee-script/#installation>

And Node.js can also be run natively:

[https://github.com/joyent/node/wiki/Building-node.js-on-
ming...](https://github.com/joyent/node/wiki/Building-node.js-on-mingw)

