prose :: and :: conz

A Sip of CoffeeScript

This month at HuntFunc, Dave presented the JavaScript library Bacon.js while using CoffeeScript. Although the focus of his topic was on Bacon.js, this motivated me to try CoffeeScript myself. It has been on my radar, but I’ve yet to dabble in it. Combined with my recent lamentations on GWT/Java/MVP, I decided to give it a try.

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 coffee executable was always hidden. Any time I did something that didn’t compile, I got the same feedback that you get when you write some JavaScript that doesn’t work… no feedback.

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.

Olde Comments
  1. Adam Holden says:

    Typescript > CoffeeScript.

    If you are going to use a JavaScript compiler, might as well use one that gives you static typing and true inheritance etc. Even though you are stuck with VS or a handful of alternatives.

    Caveat, I haven’t more than dabbled in either, but CoffeeScript just seemed like a bunch syntactic sugar with not a lot of substance. That and making it more Python-y. Typescript feels a lot more like JavaScript. I won’t revisit CoffeeScript, but I will revisit Typescript.

    Main problem with either of these is that, while compatible with other JS libraries, you are either going to have to write those sections in Javascript, or with trial and error CoffeeScript/Typescript. I think that TypeScript handles it better, though with more complexity, by allowing you to write documents that convert JS libraries to be statically typed.

    • barnesjd says:

      So TypeScript can have the expected types of the JS libraries it calls by giving a way to convert? I suppose that’s the only way to do it. Without something like that, a type-safe JS-targeted language seems rather fruitless.

      I agree that CoffeeScript is a big bag of sugar on JS. I think that can be a good thing as it allows you to do JS without shooting yourself all the time.

  2. snemarch says:

    The neat thing about typescript is that you can use existing JS libraries as-is… or you can annotate them, and this can be done in files separate to the original javascript files. People have already written definitions for quite a few libraries, check out .

    I hope more people will embrace typescript, and that there’ll be decent non-VS tooling for it sometime soon.

    • barnesjd says:

      Very nice repo. Thanks for sharing. I feel like quite a noob for not knowing about TS. FWIW, I’ve spent most of my development career on the backend, so yeah… basically a noob in the browser.

  3. […] to be better at using functional programming in a language I spend a lot of time in. Granted we use CoffeeScript, it is so semantically similar that I’m certain all of the skills will […]

Tagged with: scala (41), web-development (19), polyglot (7), javascript (6), coffeescript (3), huntfunc (2)