datamaskinen

Thomas Frössman's blog on programming, technology, tools, or something like that.

Coffescript Is Awesome

| Comments

The unbeauty of JavaScript

Every Serious attempt I’ve made at liking JavaScript has almost always been put to an end by it’s messy syntax. About A Year ago, after yet again getting involved in a project where JavaScript was needed I thought to myself why anyone hadn’t invented a sane cleaner JavaScript syntax precompiler, there turned out to be a couple of them that looked good promising.

I Have in recent years professionally mostly been working with Java for any larger projects since I am quite comfortable with how that platform works on a lot of levels. On top of that I have also been trying out smaller projects using stuff like python (django, fabric and writing utilities), haskell (xmonad), groovy (gradle) and ruby (rails, jekyll).

Having some experience in that mix of languages plus some analysis in mind I very quickly settled on CoffeeScript.

Thoughts on code generation and CoffeeScript

There exists many editors and IDEs with carefully designed language support that somewhat helps the situation. For every fancy editor feature you use the editor itself moves closer to becoming a kind of keyboard shortcut oriented DSL or code generation tool that many times just makes up for some inefficient language syntactic expressions. My history with code generation tools is mixed, I’ve worked with really good ones while others have been painful to use. From my experience, a DSL/cross compiler usually needs to either provide an significant productivity gain and/or match the target platform(s) in an intuitive way.

CoffeeScript maps very close structurally to JavaScript and adds only some small things on top of it. This makes it easy to do things like debug generated java script code in the browser while effortlessly mapping the generated code to the CoffeeScript source files manually in my head (it’s a quick learning curve). By being light on extra language features CoffeeScript also avoids adding any default run time libraries or other dependencies to projects using it, it’s just JavaScript. It took between one and three weeks before me and my programmer project mate at work were almost fluent in writing CoffeeScript and had no problems intuitively debugging the generated JavaScript code.

An Simple example

A very simple comparison of a trivial function.

JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Log an error to the console and defer the displaying of
// an alert until the end of the current execution context.
var logError;
logError = function(message, level) {
  console.log("error: " + level + ": " + message);
  if (level > 10) {
    setTimeout((function() {
      alert(message);
    }), 0);
  }
};

// Test the alerts
logError("an unimportant error");
logError("an serious error", 100);

Translated to CoffeeScript

1
2
3
4
5
6
7
8
9
# Log an error to the console and defer the displaying of 
# an alert until the end of the current execution context.
logError = (message, level) ->
      console.log "error: #{level}: #{message}"
      setTimeout (-> alert message), 0 if level > 10

# Test the alerts
logError "an unimportant error"
logError "an serious error", 100

Not sure what to think?

Check out coffeescript.org where you can try CoffeeScript out in your browser.

UPDATE: I recently found a panel interview article, Should you learn CoffeeScript?. The author of CoffeeScript and others have their say. It might help you decide if CoffeeScript is something for you.

Comments