My first impressions are pretty good. It felt a bit like Scala with the optional periods and parentheses. The lambda syntax is also reminiscent, substituting a dash for the equals. Furthermore, it is semicolon-free, which the cool kids dig.
I quickly discovered that I really like splats. It’s the equivalent of a Java varargs. By itself, it’s no big deal. The cool factor is in how it can be used for list deconstruction. This allowed me to do some recursive handling of a list I had in my code. A recursive summation of a list (presumably, but uncheckably, of integers) would look like this:
add = ([x, xs...]) -> return 0 unless x? return x + add(xs)
So yeah, here I go again… I wish that it could be type-safe. I’m not so sure that’s possible/reasonable for any language that will interop with JS. I believe I’m willing to accept dynamic typing as the nature of the beast for client-side code.
Then there is the Pythonically-significant indentation. I waver on this language feature like I waver on targeting JS for compilation. It does have the nice benefit of enforcing a common coding standard. While I like my own code to be consistent, I’m not one to push my code style or support code format standards. I believe in freedom and responsibility, not institutional control. So it’s good for consistency but bad for wielding control… Eh… I’ll accept it for what it is.
One of the biggest motivating factors for my interest in CoffeeScript is how well it works with JS. You can seamlessly utilize any JS libraries, and JS can utilize CoffeeScript because of how it compiles neatly to JS. I’m not fundamentally opposed to writing raw JS; it’s just all the weird shit that gets me. I’m always messing something up. I’ve found that CoffeeScript seems to be a great way to utilize all of the power of JS, but without all of the pitfalls and gotchas that it is riddled with. CoffeeScript is still close enough to the metal that I can stay up with the latest JS libraries and browser capabilities. For instance, I was able to utilize jQuery and Jasmine without any problems.
Now a few caveats. The CoffeeScript eclipse plugin is rather flimsy. It’s currently an inactive project looking for a dedicated owner. Eclipse also poses its usual challenge where I can never quite kill off all of the tab characters. Once in a while, it will slip one in rather than four spaces. This is a big deal to CoffeeScript because the compiler bombs when it encounters a tab. This then leads me to warn you regarding the use of the
--watch switch with the compiler. This causes the
coffee executable to run indefinitely, compiling your code changes any time you save. This is dang handy, but can lead to some frustration when you save a code change that doesn’t appear to cause any difference in behavior. Since I did this work on a laptop, the console running the
Overall, I enjoyed that sip of CoffeeScript. Soon I’ll have a blog post regarding what I actually built with it. You’re welcome to a preview of the source over on GitHub. Next time I need some JS, I will very likely reach for another cup of CoffeeScript.