10 ms·
This is CoffeeScript
- franze 15y agonote: below the pics is an actual readworthy article.
- pointyhat 15y agoThanks for the heads up. I didn't get that far initially.
- dstein 15y agoThe problem with Coffeescript is if you already know JavaScript, it's completely unreadable. Looking at code like this, I have absolutely no idea what it does: class ItemView extends Backbone.View render: -> @options.items.each(@renderItem) renderItem: (item) => @el.append item.get("title") It doesn't make sense because there are magic variables appearing out of nowhere - @options, @el, append -- what are these? I'd have to study the resulting JavaScript to understand what it's really going to do.
- qrush 15y agoHow is that not the case for any language? (This also might be a bad example, but it was based off of code I wrote)
- davidw 15y agoJust as a data point, most "on top of" languages seem a bit more distant from what they sit on top of. In other words, C is somewhat cross platform, and much higher level than assembler. Ruby does way more out of the box than C does. Coffeescript seems to mostly just be a modified JavaScript syntax, although I have to admit I haven't looked at it that closely.
- SoftwareMaven 15y agoThat's all C++ originally was, too. Everything has to start somewhere, and fixing some of JavaScript's warts is as good a place to start as anywhere.
- dstein 15y agoCoffeescript is adding an abstraction level, but all the complexity of JavaScript remains, so while you write Coffeescript, you have to sort of compile it in your head. I could maybe get on board if it went one whole level of abstraction further.
- adambard 15y agoI disagree; I think the more standard handling of `this` and the automagic variable scoping, constitute a significant reduction in complexity for people coming from other OO languages. Certainly for me, having more experience with Python and Ruby than Javascript. The abridged syntax is just gravy.
- r00k 15y agoI'd have to study the resulting JavaScript to understand what it's really going to do. Once. And then you could use shorter, more convenient syntax for the rest of your life. Worth it?
- deleted 15y ago[deleted]
- jashkenas 15y agoI'm afraid you're getting tripped up by the Backbone.js library, not the language itself... `this.options` is the options object that was passed when the View was created, and `this.el` is the default DOM element that wraps the View. For more info, see: http://documentcloud.github.com/backbone/#View-constructor http://documentcloud.github.com/backbone/#View-constructor http://documentcloud.github.com/backbone/#View-el http://documentcloud.github.com/backbone/#View-el
- Cushman 15y agoThis is the major problem with CoffeeScript: it seems so straightforward, there must be something wrong with it.
- randomdata 15y agoThe same could be said of any language you are unfamiliar with. Ignoring the inheritance, which should be familiar to anyone with class-based OO experience, that code basically translates to: function ItemView() { this.render = function() { this.options.items.each(this.renderItem.bind(this)); } this.renderItem = function(item) { this.el.append(item.get("title")) } } In this example, I don't see any significant difference as far as readability goes, to be honest. I will note, in case it is not entirely clear, that the use of Backbone in the example is unrelated to CoffeeScript.
- mgutz 15y agoExcept you left out setting the prototype chain to extend from the super class. CoffeeScript expresses it succinctly. The example above, while readable, already has much more noise than signal.
- maccman 15y agoAlso, HNers may find my open source book on CoffeeScript useful: The Little Book on CoffeeScript http://arcturo.github.com/library/coffeescript/ http://arcturo.github.com/library/coffeescript/
- dmix 15y agoI've used CoffeeScript extensively with a backbone.js project I was working on. I really liked it (and backbone). But I haven't been able to bring it into my main rails app since I'd like to have all of my JS consistently in coffeescript. I don't like having both JS/CS, just like I don't like having HAML/ERB mixed together. It would be a big project migrating it over and not as simple as HAMLizing a project. Maybe one day I'll do it, or wait for a future rails projects.
- DevX101 15y agoThere's a javascript to coffescript converter: https://github.com/rstacruz/js2coffee https://github.com/rstacruz/js2coffee Although I did have a few problems with some more complex code, it seemed to do the trick for more straightforward javascript. You'll lose your comments, but it's worth checking out.
- seanalltogether 15y agoIt seems like the removal of parenthesis is a bad idea, because it's bound to create a bunch of inconsistent code. Notice how all the rest of the code samples on the page keep the parenthesis in?
- qrush 15y agoYeah, I'm undecided yet as to which is best. My colleagues seem to be promoting keeping the parens in. Typically when we try stuff like this I like to push it to the max to see how it feels, then scale back.
- bstar77 15y agoI approach this the same way I approach Ruby... I only use parenthesis when they improve readability of a confusing statement or are required (function call with another function call-with params-as a param). As you can see, this statement required parentheses ;-)
- stdbrouw 15y agoIt does create some inconsistency (even though CoffeeScript is in many ways one of the most consistent syntaxes I've seen) but you gain a lot in readability sometimes, and it makes it so easy to craft great domain-specific languages.
- Cushman 15y agoI find it's a good smoke test for readability. Use parens to bind up atomic expressions in a line. If there are too many parens on a line, it's a good sign that you should break it up into two instead. It wouldn't be better lookin' with more.
- freddealmeida 15y agopoints for using 300 to explain coffeescript
- freddealmeida 15y agopoints for using 300 to explain coffeescript
- akdetrick 15y agoI don't subscribe to the notion that you can write code faster with less keystrokes (vi/emacs, anyone?). I do however, understand that a cleaner syntax would lend some readability and simplicity to your code once you learn said syntax. Ultimately, I find that the problems that CoffeeScript solves, and solves quite well, are not really that big of a deal for me given that I've been working with Javascript enough to get used to the quirks. I'm not sure if I'll ever use it myself, but I can't blame anyone who would; this article does a good job explaining some of the benefits.
- seanalltogether 15y agoI agree with this. As a counter example I can completely understand why some people use GWT because it provides a development infrastructure and code enforcement that you just can't get from normal javascript coding. I can't say that CoffeeScript provides enough of a change in discipline over normal javascript coding.
- mamp 15y agoThe problem with Javascript is that because of political reasons it has only evolved slightly in the last 12 years, so it takes a lot of effort to avoid the bad parts. I about CoffeeScript as JSLint + Syntactic Sugar. Both of which mean you need less heroics to get Javascript right.
- danenania 15y ago"Ultimately, I find that the problems that CoffeeScript solves, and solves quite well, are not really that big of a deal for me given that I've been working with Javascript enough to get used to the quirks." I wouldn't expect you to believe me until you've used CoffeeScript for a little while, but I think you're definitely wrong about this. CoffeeScript simply has better syntax than plain js in every way, and any js developer will be more productive using it. The only drawbacks are dealing with file conversions and debugging, but these are something like a 10% penalty at worst on top of a 100%+ productivity boost. It's not just an aesthetic preference thing. CoffeeScript is a much better language and is pretty much guaranteed to save you time and mental effort.
- bilalhusain 15y agoWhen I first checked out coffeescript, I had a feeling that Jeremy was unfairly trying to show it off as cool by being less verbose than javascript - a feeling I found echoed in one of the tweets by Zed. But after actually using it, I realized that I had missed the whole point. Coffeescript has that ruby-esque intuitiveness. A lot of time I refactor code on a gut feeling that it'll work, coffee -p, yup it does ... 9 out of 10 times. closure, is, in, of, soaking null references lets you focus more on your code rather than on patterns, handling corner cases and avoiding mistakes; and I just discovered the fat arrow thanks to this post. (Yup, the documentation is all there on the homepage but, agree or not, learning is incremental and involves mistakes and serendipity.)
- jashkenas 15y agoDug up Zed's tweet: https://twitter.com/#!/zedshaw/status/101746443949322240 https://twitter.com/#!/zedshaw/status/101746443949322240 ... which does indeed miss the point -- the side-by-side comparison on the homepage is not to show you what the equivalent handwritten JavaScript would be -- it's the actual JS output compiled from the CoffeeScript on the left. More of a "nothing up my sleeves" and no bullshit maneuver. In a perfect world, CoffeeScript would compile into the JavaScript you would have written in the first place -- but that's quite a tall order.
- bilalhusain 15y ago<public_display_of_affection> Dear sir, I'm jumping on my bed right now out of ecstasy, for my unworthy comment has garnered a reception, from a noble soul like yours. You just made by day. In fact, you do it through the means of coffeescript everyday. Thank you for coffeescript, kind sir. Hats off. A broke, insecure, forever alone developer. </public_display_of_affection>
- Cushman 15y agoEspecially since if I had to write that JavaScript by hand, I wouldn't.
- 15y ago
- funkah 15y agoI agree that "function" is too long, but I'm not a huge fan of these arrow keywords because they're not very easy to type. But something like TextExpander would make a simple fix.
- stdbrouw 15y agoNot easy to type? What kind of a weird alien keyboard do you have? :-)
- chc 15y agoThe arrows are easy enough for me. Middle finger hits the - or =, pinky hits shift and index finger hits >. It's drastically less finger-work than f-u-n-c-t-i-o-n-shift-(-)-space-{-}. The only languages I can think of off the top of my head with easier syntax for anonymous functions are Scala (a pair of parentheses is literally all you need) and Clojure (with its #() syntax), which both seem to have put a lot of effort into making them easy. In particular, Ruby and Python both require more typing. At their most concise: Ruby: -> {} Python: lambda:
- thezilch 15y agoYou may be stuck with your IDE: JavaScript: fu<tab><shift-)> function(){ <cursor> } Python: la<tab>: The remaining characters should be filled in by most modern IDE / IDE plugins.
- chc 15y agoThose are both still more work than ->, and you could make a shortcut for the arrow as well if it's so difficult. Anyway, my point is that I don't see why the arrow should give anyone with a normal keyboard layout an inordinate amount of trouble compared to similar constructs in other languages.
- dreamdu5t 15y agoThe bottleneck is never the time it takes to type the code. Honestly, CoffeeScript is neat but it's not much of an improvement over JS. It's just as ugly and unreadable. It's only an improvement if you're counting characters. I don't find CoffeeScript any less ugly or readable than JS. It seems incredibly redundent.
- alfbolide 15y agotrue we should all write machine code.
- rgbrgb 15y agoAgree to disagree about what is UGLY.
- statictype 15y agoIt's not about the time it takes to type code. Code that requires less typing is more succinct and therefore easier to read and understand. You don't find it easier to read. Fair enough. I think a lot of people do. I personally found it much easier to write structured code in Coffeescript than in Javascript straight.
- danenania 15y agoI'm sorry but what? Are python and ruby ugly and unreadable? Are they on the same level as js? Because coffeescript syntax is essentially a combination of the best aspects of both with a cleaner lambda syntax. I'd love to hear which languages you find beautiful and readable if cs doesn't meet your standards.
- alfbolide 15y agoThe fat arrow by itself make it a mandate for everyone who wants to write OO js write Coffeescript.
- collint 15y ago"Just like JavaScript but nicer to type." was a decent pitch to make CoffeeScript sound safe. But it's become so much more. And you get past the simplicity of that pitch quickly if you use CoffeeScript earnestly. Going through the Table of Contents on http://jashkenas.github.com/coffee-script/ http://jashkenas.github.com/coffee-script/ I've picked out the features of CoffeeScript that make it a different beast than just a JavaScript without warts. * Lexical Scoping and Variable Safety * Conditional Assignment * Splats * Comprehensions * Everything is an Expression (&implicit return) * The Existential Operator * Classes, Inheritance, and Super * Destructuring Assignment * Function binding * Chained Comparisons * Extended Regular Expressions This is beyond a simple sweetener for your JavaScript code. Yes, you could write your JavaScript like you write your CoffeeScript, but you wouldn't. Try this little jewel in Try CoffeeScript and you'll see what I mean: [open, contents..., end] = "<impossible>".split "" alert contents.join ""
- boucher 15y agoI think the jury is still out on whether or not the existential operator is too clever for its own good (in particular when used as part of a chained expression).
- collint 15y agoWhat is so clever about a nil object check? I suppose if you're using it to make skirting the "Law of Demeter" and thats something you don't want to do it could seem like something bad. But I'll take "foo?.bar()" over "foo.bar() if foo" gladly.
- boucher 15y agoYeah, I can see the argument in favor of it. Easy to miss when reading through code, but arguably unlikely to be used on a way where it would really matter if you missed it while scanning. I found myself trying to decide whether or not to use it the other day and decided for now it was too clever. So I guess the jury is out in my own head, maybe not in anyone else's.
- 15y ago
- statictype 15y agoMy only major complaint with Coffeescript is that it doesn't have a construct for handling the context-sensitive 'this' keyword. I still have to write self= @ at the top of functions. The fat arrow for binding 'this' certainly helps but when you have to be really careful when writing code that nests several functions. Also, would be nice if the for-loop had a way to automatically wrap the block in a function without having to write for(var x in lst) do (x) -> whatever(x)
- JoelMcCracken 15y agoNot sure that I understand you. Do you mean that you find it odd to use =>, and thus resort to self=@ for your callbacks? Also, sometimes I do $.each lst, (i, item) -> whatever x because I always use jQuery in my projects.
- d4n3 15y agoI often have to resort to self = @ when using jQuery in classes. Because of how jQuery binds the queried element to this, and the fat arrow overrides this, there doesn't seem to be another way to access both your class and the jQuery element in the same function. for instance: class ButtonLogger constructor: -> @clickedButtons = [] $(document).ready => @init() init: -> self = @ $('button').click -> $button = $(@) self.clickedButtons.push $button.attr 'id'
- jashkenas 15y agoTry `event.currentTarget`, and `event.target` instead. They're much preferable to having a `self` (representing some random DOM element), and a `this` within a function. $('button').click (e) => @clickedButtons.push $(e.target).attr 'id'
- M1573RMU74710N 15y agoI'm not sure I understand your first complaint. The fat arrow is a great helper and saves a lot of work in various situations....obviously it doesn't cover all situations...but in the situations it doen't cover I'm not sure what I'd expect the language to do. Could you give an example of what you imagine the feature to be? I'm just not sure if something as simple and easy as that = @ needs improvement. Anything beyond what that and => covers, to me seems like something a library should handle...and CS makes that very easy. For you second one, what about do ( -> whatever x ) for x in foobar One of the things I like about CS is that it is very conservative about not adding helpers for every little thing you can think of. It chooses a few very powerful and fundamental building blocks, and let's you use those to build up yourself. I can respect some people wanting more though.
- donpark 15y agoFirst week of using CoffeeScript, I was mostly writing code and it felt foreign. Second week, same but it now felt convenient and I used it like a DSL. Third week, I was reading code as much as writing it and it suddenly became essential. CS does have some rough spots that feels at odd with rest of the language but, overall, it brings clarity and convenience at minimal cost.
- scubaguy 15y agoPerhaps the biggest strength of CoffeeScript is that it takes JavaScript, where there are many different ways to do OO inheritance; write and use functions; perform functional tasks such as list comprehension and provide a consistent and simplified syntax to do them. Because CoffeeScript is language designed how JS developers write code today, it feels more natural and fluid. It makes JS programming simpler.
- scdc 15y ago>var html = "<option value='" + this.id + "'>" + this.get("title") + "</option>"; >This is such nonsense. Why did we put up with this? In CoffeeScript, you can write: >html = "<option value='#{@id}'>#{@get("title")}</option>" I have to say, the first one is much easier for me to read and understand.
- tlrobinson 15y agoAm I the only one who thinks this is horribly difficult to visually scan? item = new Item title: "Awesome", id: 2 Less isn't always better. I like the parens, at least if there's more than one argument. item = new Item(title: "Awesome", id: 2) It's much easier for me to glance at this and know what's going on.
- jrockway 15y agoWhat else would it mean?
- tlrobinson 15y agoI can understand what it means, it just takes slightly longer to comprehend without the parens providing a visual cue that it's a function call. It only gets worse with nested function calls: item = new Item title: new Title "Awesome", id: 2 The syntax isn't ambiguous, but that doesn't mean it's easy for humans to parse.
- deleted 15y ago[deleted]
- jessedhillon 15y agoIn a language where there are two drastically differing ways to express the same idea, it could mean anything.
- jrockway 15y agoHow are we having this conversation in English, then?
- jessedhillon 15y agoWell, it took about 8-12 years of practice for me to be able to do it correctly, and at least twice that to achieve my current proficiency. So when I have my choice of designed languages, I'd like one that tries to learn from the failings of the organic ones.
- 7mythie 15y agoI've been playing around with coffeescript for a month now and I'm still undecided about whether these punctuation marks improve the syntax or not. Typing out "function" doesn't really take that long and it's much more readable than "->". On the other hand, I miss the parentheses and semi-colons. Without those punctuation marks the code feels more ambiguous. Maybe I'm just stuck in my ways, hopefully something will click soon.
- Spyro7 15y agoI am a recent coffeescript convert. When I first heard about coffeescript I dismissed it offhand as a pointless abstraction over javascript, which I judged to already be a high level language. (In retrospect, I am ashamed to say it, but I think that I found the syntax to be intimidating.) I then went on to continue, happily, writing thousands of lines of javascript. Somewhere along the way, I came across Paul Grahams essay "Succinctness is Power" ( http://www.paulgraham.com/power.html http://www.paulgraham.com/power.html ), and I thought about how my productivity could be increased if I had access to a language that offered me the ability to do more with less. That was when I decided to revisit Coffeescript. For my first task, I decided to rewrite some of my simpler javascript code into Coffeescript just to get a feel for it. Initially, the things that had initially repulsed me were somewhat irritating: * The lack of parenthesis and brackets - I doubted that it would be possible to maintain code readability without them * The reliance on indentation * The overall "strangeness" of the appearance of coffeescript code (to someone with a predominantly java background) Here's the thing though. All of those objections are only surface deep. Once I actually started to code in Coffeescript, I found that I became completely comfortable with the syntax. As a matter of fact, I don't think that I could ever go back to conventional javascript again. Especially because I would lose (among other things): * An elegant syntax for writing classes using javascript * List comprehensions * Elegant string interpolation (it's the little things that count) It is easy to be skeptical about coffeescript if you natural lean away from things that are surrounded by hype, but, take it from me, coffeescript really is an extremely valuable tool that no web developer should be without. It is more than just a way to write pretty javascript. It is a powerful, flexible, and elegant language in its own right. Don't be turned off by its syntax, give it a try today!
- perfunctory 15y agoanybody wants to guess what this code compiles to? a [1] = 2
- TrevorBurnham 15y agoI'm the author of the PragProg book on CoffeeScript (http://pragprog.com/book/tbcoffee/coffeescript http://pragprog.com/book/tbcoffee/coffeescript), and I run @CoffeeScript on Twitter. It's always fun to see a "CoffeeScript rocks" article like this explode on HN and Twitter. But it's even better to see stuff that people are building in CoffeeScript. The language has reached the point where it's gaining real traction in production settings, and not just among the beautiful code addicts at 37signals. There was a YC job posting about a week ago for a "CoffeeScript drinking frontend engineer" (http://news.ycombinator.com/item?id=2877677 http://news.ycombinator.com/item?id=2877677); they're also using CoffeeScript for a Node.js server, and even for MongoDB queries. I'm really excited to see that become a more common occurrence.
- jvoorhis 15y agoWhy raise so much snark straightaway about keywords, and then gripe about the silly ones later? I've written thousands of lines of JavaScript, and a coupla hundred of CoffeeScript. CS is a usable, rather polished JS overlay, but I only understand it in terms of JS. In any case, I know which will be around longer.
- MHBerryman 15y agoJust thought I'd share the CoffeeScript + jQuery article in Septembers issue of Linux Journal: http://www.linuxjournaldigital.com/linuxjournal/201109/?pg=32&pm=2&u1=friend http://www.linuxjournaldigital.com/linuxjournal/201109/?pg=3...