In Which I Tell You to Stop Whining About CoffeeScript

For example, at Posterous, they use [CoffeeScript] but don’t have any real JavaScript engineers.

— Oscar Godson in Should You Learn CoffeeScript?

 

Hi, I’m Adam. I’m an imaginary JavaScript engineer, formerly from Posterous. One day, I hope to be a real JavaScript engineer.

CoffeeScript is a controversial language, and will continue to be for some time. If you’ve never heard of it, you should check it out. Its inventor, Jeremy Ashkenas, defines CoffeeScript as a “little language that compiles into JavaScript.”

Let me be clear: I am a JavaScript engineer. I know how this works. I know about polluting the global namespace. I know about prototypical inheritance. Yet why do I continue to promote a language that is described by some as a “crutch” for novice JavaScript developers? Because CoffeeScript saves me time, plain and simple.

While using the language is ultimately a personal choice, I want to address some of the numerous objections to CoffeeScript.

More than sugar

Many object to CoffeeScript because they perceive it as fixing a very small problem: JavaScript’s somewhat verbose syntax. While this is one of the many benefits of the language, I actually don’t think this benefit alone is so insignificant. Consider the anonymous function syntax in JavaScript. In this example, I am calling a method helloWorld, which takes one argument, a closure:

helloWorld(function() {
  alert("Hello");
});

Compare to its CoffeeScript equivalent:

helloWorld ->
  alert "Hello"

It’s obvious to see that CoffeeScript’s version is more concise, but the important thing is this: as real/imaginary JavaScript engineers, we have to create closures constantly. Having to type function () {} over and over again just seems like an exercise in masochism, and let’s not even mention the curly braces.

Yes, typed out once, this example doesn’t seem to provide earth-shattering improvements, but in aggregate, you end up saving yourself a ton of time, all while increasing the readability of your code.

This also applies to not leaking variables into the global namespace, and constructing for loops. Why bother caching the length of an array in a for loop if the language takes care of that for you automatically? Novice JavaScript engineers aside, the language niceties of CoffeeScript save the advanced engineer a ton of time. There’s no arguing that.

“CoffeeScript makes JavaScript hard to debug in the browser”

The main argument I hear goes something like this: “If I’m writing CoffeeScript, and I encounter an error on the browser, I’m looking at a different language in Web Inspector. The syntax doesn’t match up perfectly, and the line numbers certainly don’t match up. Why should I have to switch contexts like this?”

While I do agree that debugging compiled CoffeeScript is slightly more difficult than debugging pure JavaScript, I don’t see how this is a problem. Any respectable CoffeeScript engineer should have a strong knowledge of JavaScript. CoffeeScript is absolutely not a crutch in any way. If a CoffeeScript engineer is expected to know not to leak variables into the global namespace, to set up for loops properly, and to understand how this works, then surely such an engineer should have no trouble seeing the compiled JavaScript and having some semblance of an idea of where the problem lies in his CoffeeScript.

Let’s be real here: It’s not like the CoffeeScript compiler creates some sort of cryptic hodgepodge of esoteric JavaScript syntax. When you write some CoffeeScript like a = "foo", the compiled JavaScript looks like: var a = "foo";. Now is that really so hard to decipher that we need to collectively wring our hands at this “horrible” language?

Let’s also not mention that the way JavaScript itself appears in Web Inspector (or Firebug) does not always match how it appears in your source. If you are using any kind of asset packager, you are almost guaranteed to have a line number mismatch.

Not an excuse to forgo learning JavaScript

When I talk to someone who is trying to learn how to DJ for the first time, I always give the same recommendation: Learn to spin on vinyl turntables first. I say this because even though you never see a vinyl setup at a club anymore, it is a layer deeper than using CDJs or your laptop to mix music.

The most valuable class I ever took during college was CS 61C. Our professor started by teaching everyone C. Then we learned how C is transformed into MIPS Assembly. After that, we learned how MIPS Assembly appears as byte code. Finally, we learned how to make a processor that could read that bytecode and produce output.

This class was valuable because by learning the underpinnings of a higher-level language, we can write better, and more performant code in that language.

Gary Bernhardt on Ruby and C:

I like this quotation because it mirrors my views on CoffeeScript and JavaScript. If you want to be an effective CoffeeScript developer, you must learn JavaScript. This is even more important than learning C as a Ruby developer, because as we all know, CoffeeScript is compiled into JavaScript, which is what actually runs in the browser.

Give it a chance

Just like anything new, it’s going to take a while before you get used to it. When I learned Ruby, the whole concept of the language was completely foreign to me for many months. After getting the hang of it, though, I fell in love.

Oscar Godson again:

I tend to be more critical of things before I’ve tried them, but I did try CoffeeScript to see what all the hype was about. It was nice, but it’s not worth it.

But how long did Oscar Godson give it? A month? A day? Five minutes? I eschewed CoffeeScript too the first few times I saw it in the wild. I thought of it as a “toy language for novices” for a very long time. It wasn’t until I committed to really learn the language and use it in a personal project that I came around.

Ultimately the decision to use CoffeeScript is a personal one. I disagree with most objections to using CoffeeScript, but if it just doesn’t “feel right” to you, then don’t use it. CoffeeScript saved me a lot of time while developing Posterous Spaces, and it continues to save me time in personal projects. If your initial reactions to this language were knee-jerky in nature, I urge you to give it another chance.

tl;dr

CoffeeScript isn't just for novices. Its syntactical conveniences save time. Stop whining and give it another chance.