LiveScript <| Rocking your world.js

cover-image

I presented a TechSession(tm) to my development team last week which was meant to be about LiveScript. It turned out that in order to understand what the purpose of something like LiveScript is, you need to understand the beauty-warts of JavaScript (oxymoron intended).

Basically, JavaScript is completely awesome. It's multi-paradigm (procedural or object-oriented or functional), everywhere, asynchronous and dynamic. Not everyone's cup of tea, but certainly a very strong choice for a broad range of programming tasks. However there are a couple of severe gotchas, which stem from it's long and sometimes weird history.

1. Type coercion oddliness

JS is the king of coercion. It can coerce anything to a string basically, resulting in some surprising results.

2. Scope traps

Variables are defined global by default (x=5;), unless you attach the magical "var" keyword to them (var x=5;). Ouch.

if ('1' == true) {  
    x = 5;     // win! you have polluted global
    var y = 5; // fail! you kept it local...
}

3. Surprising behaviour

A for-in-loop over a list returns the index into the list instead of each element, BUT a for-in-loop over an object returns the key to the object (win). Ok, ok, if you understand that the for-in-loop always returns the "index" into the list or "index" into the object thats totally consistent. The point is that I often find myself writing little mini for-in-loops while i'm developing to remind myself of what exactly is returned - meaning it's obviously weird :)

4. Thit and that

Anyone who's written object-oriented style in Javascript would have discovered that either they're going mad, or JS is broken in the "this" department. Some people argue that "this" is the "functional scope" and therefore perfectly reasonable that it's set to whatever-the-hell-it-wants, but i'm not convinced that "this" should more reasonably be a closure-captured variable of the outer scope (if it's available) and that some other magic variable called "functional_this" should be set to the functional scope (exactly how every other closure-captured variable works!). I think it's a bug that no-one will admit to :)

Enter [coffee|live]script

So coffeescript and livescript profess to fix up all the issues in javascript - but simply abstracting another language layer over javascript, but fundamentally compiling to javascript. They both work extremely well at this and I totally recommend you use one of them over plain javascript to avoid a shed-ton of simple developer bugs resulting from all javascripts dev traps.

In my opinion coffee does a decent job, but promotes a more imperative style whereas livescript does an even better job, and promotes a more functional style. If you are comfortable with functional languages (specifically F# or Haskell) - then livescript will seem very familiar. If not, and you are alergic to functional, then probably best to stick with coffeescript to avoid any unnecessary high blood pressure...

I've upped the slides to SlideShare, which you can open directly or view the embedded version below: