$('.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.