Why I Like

Why I like CoffeeScript

If you haven’t heard of it, CoffeeScript is “a little language that compiles into JavaScript.” It’s written by Jeremy Ashkenas, and reminds me a lot of Ruby. Also, try getting out of that cave once in a while. Anyway, in the past couple weeks, I’ve had conversations with at least three different people about it, and they all said something to the effect of, “why would you use that? I don’t understand its purpose: it’s just one more thing to compile.” So, I’m going to explain to you why I like CoffeeScript, and then I’m going to have to spend the rest of the post explaining my explanation. Ready?

I like CoffeeScript because it’s not JavaScript.

That’s it, really. I don’t like JavaScript. I think it’s ugly, I think it’s too verbose (typeof foo !== "undefined" && foo !== null makes me die a little inside), prototype-based programming seems alien to me, and I’ll be honest, front-end work kind of freaks me out. I’d rather deal with datatypes on the server than DOM in the browser any day. It’s probably that last one, more than any of the others, that’s kept me away from JavaScript all these years.

Two things convinced me to change my mind and give front-end development a try (and at the risk of sounding like a total fanboy, they were both written by the same dude. Totally a coincidence, I swear). They are CoffeeScript and Backbone.js, but once again, Backbone is a subject for another day. So that leaves CoffeeScript.

First, I appreciate how expressive CoffeeScript is. Scratch that, I appreciate how expressive Ruby is, and I appreciate that CoffeeScript does its best to take the best parts of Ruby and apply them to the problem of writing JavaScript. The fact that I can do something like this (example from the CoffeeScript wiki entry on writing DSLs)…

… and have it come out the other side as valid JavaScript just blows me away. Implicit parentheses and the “everything is an expression” mentality are awesome.

Second, I appreciate the effort that CoffeeScript makes to be concise, and to abstract away the ugly parts of its target language. My if(typeof foo !== "undefined" && foo !== null) example from earlier, for instance, becomes simply if foo?.

Third. Yes, it’s one more thing to compile. And? Don’t you have build and/or deploy scripts or something? Also, if you’re using Ruby or node, you can watch your .coffee files for changes and magically compile them without your having to lift a finger (except, you know, the fingers you lifted to tell the compiler to watch the files in the first place. Shut up). I guess it really comes down to a cost/benefit analysis: if you think the features of CoffeeScript that I’ve mentioned are worth it, you won’t mind compiling. If you don’t, then compiling will just be one more reason not to use it.

Update: here are a few more that sprang from conversations this morning with the same friends mentioned above. You’re welcome.

CoffeeScript takes care of declaring your variables for you, in their proper scope, so you never have to worry about hoisting and undefined variables.

Implicit returns. CoffeeScript will turn the last line of your functions into return statements. That’s right, whatever line is last in your method body will be evaluated and sent back to the calling code (though you can use return as your last line to keep the default “return void” behavior).

This might seem like a negative if most of your JavaScript methods return void and you’re not used to thinking that way, but trust me: it’s a good thing. There’s a comment by jashkenas on an issue in CoffeeScript’s GitHub repository that explains the reasoning behind it. To sum up:

… CoffeeScript forces you to adopt the habit that you have to think about having a good return value for every function you write — and I think it’s a great habit to have. There’s a lot of JS out there that would benefit from better return values, even for side-effect-ful code. …

What? A language that wants to encourage us to become better developers? Preposterous!

End update.

To sum up (and answer my friends’ questions): you would use CoffeeScript when you want to be able to write JavaScript more expressively. You would use CoffeeScript when you’d rather solve problems and let somebody else write (the ugly parts of) the code. You would use CoffeeScript when you want to be awesome. Okay, that last argument might not hold up in court. So anyway, yeah. I like CoffeeScript because it’s not JavaScript, and now you know why.

4 thoughts on “Why I like CoffeeScript

  1. Great post. I’d be interested in seeing what the CoffeeScript compiles to in your example. This is much more readable than the JavaScript I expect it to become, and for that I am, well, interested. As a back-end developer, I see a lot of what I like to see in code. As a front-end developer, I’m still a little unsure, since jQuery is my “language” of choice, and it’s met my needs to such a huge extent, but I’ve often been irritated about the typeof thing.


    1. Sure. You can play with all the CoffeeScript used in this post here. Note that if you want to actually execute the script, you’ll need to implement the define() method.

      And to address your second point, CoffeeScript isn’t meant to be a replacement for jQuery or anything else. Since it’s “just JavaScript,” it’s compatible with whatever js libraries you happen to be using.

  2. Also, I can’t really complain about the compilation thing. I would probably consider that a feature, since it’s not “compiling” as in “to binary”, it’s more “converting” to use the standard. Take, for example, Less. Less is an excellent tool for CSS that keeps your CSS organized and makes it easier to modify. It compiles to CSS. It’s not like it takes a long time, it just has to convert itself to the ugly stuff to be used by the standard tools for all browsers (mine wouldn’t be ugly, but, you know, comparatively… or if you wrote it).

Leave a Reply