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.
glass = new Whiskey
console.log glass.age # 18
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
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).
… 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!