I love JavaScript, like really, really, love JavaScript. I love it’s crazy functional scoping, its split personality when it comes to inheritance, and of course its async nature. JavaScript in many ways is a language that should make no sense at all, but for some unknown reason deep down in my psyche it makes perfect sense. No other language makes me feel as if it has a soul like JavaScript. So when CoffeeScript came out I immediately dismissed it as an attempt to fix something that was already perfect.Many months have passed since CoffeeScript was introduced but I remained a sideline hater. I had many reasons for not liking CoffeeScript, but most were born out of ignorance. Today I decided to leave the comfort of my ignorance and finally give CoffeeScript a chance. I decided porting one of my JavaScript modules over to CoffeeScript would give me the best objective comparison of readability, code quality, and ease of debugging. Here are the final git repos of my infiniScroll module written in JavaScript and now CoffeeScriptJavaScript: https://github.com/joneath/infiniScroll.js
CoffeeScript: https://github.com/joneath/infiniScroll.coffeeHere’s the short list of issues I had while working on the CoffeeScript version.
1. I felt like I was having to fight CoffeeScript to provide private variable support in my objects closure. Writing a bunch of myvar = undefined, myvar2 = undefine, etc. at the top of my module just so I could get the private scoping correct felt like I was trying to jam my JavaScript patterns into CoffeeScript. I inevitably decided to just attach everything to “this” or “@” and make everything public :(
2. Ruby style automatic returns are awesome but I found that most of my functions do not need to return anything. This left me with a bunch of weirdly compiled code with return statements being abused. I tried forcing a return of undefined to clean up the compiled code but then my I CoffeeScript code had needless return statements everywhere :(
https://gist.github.com/2617303
Gist showing crazy returns in compiled code if empty return statements are left our of CoffeeScript.
https://gist.github.com/2617423
Gist showing crazy empty return statements in CoffeeScript to remove unneeded returns in compiled code.
3. Executing a typeof in a ternary resulted in horribly insane compiled code.
https://gist.github.com/2618020
4. Debugging code sucks! My Jasmine tests tells me error at line #134 so I have to look at the compiled JS file to get context of where that is in my CoffeeScript. This constant loop of Jasmine to JavaScript to CoffeeScript gets old fast! Thankfully this will be fixed soonish with the support for Source Maps in Chrome
Overall, my initial experience with CoffeeScript was positive. The few issues I did run into hopefully arose from my basic understand of CoffeeScript best practices. The only remaining concern I have is for the soul of CoffeeScript. I love JavaScript for its wacky personality and nonsensical quirks. I only hope that in CoffeeScript’s efforts to subdue some of these idiosyncrasies, the soul of JavaScript is not lost.
P.S. Please let me know how wrong I am. I would love to know CoffeeScript best practices!