Developer's Perception

Odd thoughts from a developer.

Polyglot JavaScript

| Comments

Over time JavaScript has gained a lot of traction as it is the default option for running code in the browser, and it has become a valid server option with the rise of node. This progress has largely been fueled by an arms race between the major browser vendor continuously trying to wield the fastest environment for running JavaScript.

Thus, as JavaScript can be run everywhere and with the notion that this language like any other has its rough spots (partly due to the history of it’s creation, a notion supported by the famous book by Douglas Crockford “JavaScript the Good Parts”) it is no wonder that alternatives that compiles to JavaScript have been popping up for a while.

Microsoft recently released TypeScript to join the efforts of Google with their Dart language to define the future of code running in the browser. Simultaneously, initiatives like CoffeeScript (by J Ashkenas) has gained some traction and the community is working (fighting) to get the next version of JavaScript ready. Another option that I cannot leave out is ClojureScript a version of the brilliant Clojure language that compiles to JavaScript.

The options are abundant each with their own goals. Some like TypeScript and Dart aim to bring some form of static typing to the table (I do not specifically want this in JavaScript, but I still welcome these initiatives), while a language such as CoffeeScript aim to improve the syntax and hide some of the bad parts of JavaScript.

J Ashkenas (of BackBone, Underscore and CoffeeScript fame) maintains a quite comprehensive list (https://github.com/jashkenas/coffee-script/wiki/List-of-languages-that-compile-to-JS), and for a good read on the different aspect of languages compiling to JavaScript consider: http://buildnewgames.com/compiling-to-javascript/

Indeed it is a very good time to be a web (Node) programmer. Some notable JavaScript engineers (Such as N. Zakas) claim that time would be better spent improving JavaScript and teaching it than building alternatives, but I believe that every new language will bring inspiration to developers and to the future of JavaScript.

All of the languages compiling to JavaScript has a shared number of cons:

  • Build process: Because they actually compile to JavaScript. Any serious JavaScript project will have a build process anyways, combining files, minifying, obfuscating, possibly running test suites automatically, and thus I do not see a big problem with this.
  • Debugging: Since what you are sending to the browser is JavaScript your debugging will also be in JavaScript. This means that unless you can get source map to work http://addyosmani.com/blog/the-breakpoint-episode-3-source-maps-shortcut-secrets-and-jsrun, to use one of these languages you need to be fluent in JavaScript. Definitely, a challenge for some, but for experienced JavaScript developers this might not be a big problem (on a side note I believe that CoffeeScript for instance generates quite nice JavaScript).

CoffeeScript

I have spend some energy on CoffeeScript as an alternative (or in addition to!) to JavaScript on my projects. I believe CoffeeScript brings readability and as a consequence of this also maintainability to a project. I cannot stress enough how important readability is for any piece of code and as such doing CoffeeScript to improve that becomes a very intriguing option.

One of the features about CoffeeScript that i most often hear complaints against is meaningful indentation, and it was also something I personally was not sure I would like. Nevertheless, the more I think about it, I am for using the indentation, as this is the way I consume code more than it is the braces. Why should the compiler use a different mechanism than me for interpreting code? Is it not easier to maintain code where your understand the code the same way the compiler does?

Here is just a few examples of features I believe adds readability to code:

Function syntax:

1
var a = function() { return x*x; };
1
a = -> x*x

This especially becomes valuable if you like me use a lot of underscore.js functionality:

1
_([1, 2, 3]).each(function(num){ alert(num); });
1
_([1, 2, 3]).each((num) -> alert(num));

Default values:

1
2
3
4
5
6
function(a, b) {
  if (!b) {
      b = 'b';
  }
  console.log(a, b);
}

In CoffeeScript you could do:

1
(a, b = 'b') -> console.log(a, b)

Basically, CoffeeScript is full of features that reduces the cluttering of your code removing unnecessary noise and thereby improving readability.

I could whip up a ton more examples, but instead if will just point you to http://coffeescript.org/ where you can read more if your interested. No need for me to copying all the examples :-D

Recently, I converted a JavaScript project to CoffeeScript and I litterally ended up removing one third of all the characters just in the conversion. Afterwards, I spends some time using some of the nice features such as default values for parameters and splats, and consequently was able to reduce the amount of code even more.

It is indeed a good time to be a polyglot JavaScript developer and I cheer every single time a new language can compile to JavaScript – bring it on!

Comments