3 ms·
A Python programmer’s first impression of CoffeeScript
- thegoleffect 16y agoFor "Switch statements don’t allow you to fall-through to the next case", you can combine multiple cases: when "a", "b" then doStuff()
- shazow 16y agoMy 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 16y agoCoffeescript 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 16y agoI 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.
- M1573RMU74710N 16y agoI see where you're coming from, but it seems a lot like "my biggest complaint about languages I don't know is that I don't know them". To be fair, I think there are varying degrees of intuitiveness, clarity, etc with languages but I don't think your example really demonstrates a schism in that regard between JS and CS: $('.shopping_cart').bind 'click', (event) => @customer.purchase @cart var self = this; $('.shopping_card').bind('click', function(event) { return self.customer.purchase(self.cart); }); If you look at the two together, you'll see everything is almost in exactly the same place. The only difference is some punctuation and small syntactic changes. Coffeescript should be pretty intuitive for anyone experienced with Javascript, but it does take time to learn and "train" your eyes and brain to understand the syntax...otherwise it wouldn't have much point (but this is true of any language). I think it's a great virtue of Coffeescript that the CS version is so close to your JS one, while being more readable (imho), shorter, etc. >The inconsistent use prevents me from being able to skim code and get a feel for what it's doing. Again, I can't help but feeling like this is a non-Spanish speaking complaining that Spanish prevents them from reading and understanding a Spanish language Magazine. The optional syntax in Coffeescript is generally optional for a reason, because it is determined by something else (generally indentation). I think you're right that this could be abused, and I could see how someone might not like it...but personally I prefer it. I like languages that will just do the obvious thing, and let me spell it out when I want something else. I think again your example doesn't really bear out your point, as it seems plain as day to me that each item is separate and I can't imagine needing commas to differentiate them (though I can respect you might not feel the same). In fact, contrary to helping commas, are more likely to introduce errors. One of the most common errors in JS is forgetting a comma in a list like that, or the dreaded "hanging comma" error that will only cause an error in IE. Not having commas also allow easy yanking and insertion of whole lines to reorder the list, etc. Don't get me wrong, again I think you have a point, it just seems to be that the crux of it is this style syntax is not your cup of tea (which is fine). On an unrelated note, I'd advise against using the name "self" for variables in Javascript, as it's already taken: https://developer.mozilla.org/en/DOM/window.self https://developer.mozilla.org/en/DOM/window.self
- ivanzhao 16y agoI 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 16y agoJust 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 16y agoI 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 16y ago$.getJSON url, (response) -> ... # Or, more detailed... $.ajax url: "/query.json" type: "GET" success: (response) -> process response error: -> alert "trouble!"
- bballbackus 16y agoI 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 16y agoYou don't need Cygwin to "install" CoffeeScript. You can even run it in your browser: http://jashkenas.github.com/coffee-script/#installation 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-mingw https://github.com/joyent/node/wiki/Building-node.js-on-ming...
- TrevorBurnham 16y agoThere's also a Ruby wrapper for CoffeeScript, and several projects aimed at Windows that use Java or .NET. A good round-up is at http://stackoverflow.com/questions/3175561/coffeescript-on-windows http://stackoverflow.com/questions/3175561/coffeescript-on-w...