Notes on Falsy Values

24 May 2011

I spent the majority of last week in Warsaw at Falsy Values where I attended a Games Workshop and a conference day. It was also my first week working as a Developer Evangelist working for Pusher. A big and exciting week all round.

HTML5 Games Workshop

The HTML5 games workshop was ran by @kuvos and @pornelski and we went through building a game of Tetris and then Mario. I was very impressed to see what can be achieved with Canvas and the workshop provided a lot of food for thought about the sorts of things that would be really useful to HTML5 games developers. Watch this space, and the Pusher blog.

The Keynote

Douglas Crockford at Falsy Values - and a Pusher banner!

The morning of the conference day saw a few fuzzy heads from the Pusher party the night before, but they soon cleared as Douglas Crockford delivered the keynote. He covered the good and bad parts of JavaScript and how, as developers, we need to us a combination of head and gut (instinct) when writing code. JSLint got a number of mentions and Douglas stressed the importance of following coding practices. He explained that just because JavaScript lets us do something, it doesn’t mean we should necessarily do it.  Here are a couple of examples:

Always use treble equals for comparisons

if(myVar === 0) {
}

Many of us already know that double equals can lead to problems. 0 compared with an empty string will evaluate to try, undefined compared with null will evaluate to true and a number of no so obvious examples:

'0' == 0 // true
'00' == false // true
true == 1 // true
undefined == null // true

Therefore Douglas believes it’s best practice to always use treble equals so that the code intent is absolutely clear.

Put curly brackets after parenthesis

function myFunc() {
}

This was a hard one to swallow as I like to put my parenthesis on the left - on the next line. However, Douglas provided an example which made me realise that it could be a bad thing.

function buildObjectLiteral()
{
   return // this returns!
   {
      'someProperty': '"'hello'
   };
}

The interesting thing here is that undefined will be returned because a line break is also ends a statement in JavaScript. I don’t think I would actually ever do this. I would probably assign the object literal to a variable first and then return it. However, if I didn’t follow this rule I might make the above mistake. If it can happen then the easiest way to avoid it is just don’t do it. So, curly brackets now go on the same line – on the right.

Do the right thing

Douglas explained that he has made some changes to his coding practices recently. A developer had contacted him asking if JSLint should report a problem if it found a switch statement that allowed any code to fall through between cases. Although he had a gut feeling that it was probably not a good thing to do he believed that it would “hardly ever” cause a problem. Some time later the developer got back to Douglas saying he’d found a bug in JSLint. The bug was causes by a switch statement which allowed code to fall through cases. Doug has now formed the opinion that if something can cause a problem, even if it’s unlikely, that the best practice is don’t do it.

switch(test)  {
   case 0:
      // don’t do it!
   case 1:
      break;
   case 2:
      break;
}

When will ECMAScript 6 reach the web browser?

Dmitry Soshnikov gave a talk on ECMAScript 6, codename Harmony. Whilst there seem like some good ideas going to to ES6 the question remains when will anybody actually be able to use it in a web browser? It will probably a be available in node.js before it's most browser runtimes. It also sounded like a lot of syntactical things were being drawn from Erlang - not sure about this.

Slides: http://www.slideshare.net/dmitrysoshnikov/falsyvalues-dmitry-soshnikov-ecmascript-6

Fabric.js - the new defacto JS Library for Canvas?

I didn't sit through this talk as I had to pop out and get some work done and thought my tapping on a keyboard would annoy those around me. However, whilst in the games workshop it was very clear that it's unlikely that people would be accessing the Canvas API directly and that, in the same way that very few people use document.getElementById, very few people will directly call canvas.getContext("2d") - they use a library that offers richer functionality to the developer. Fabric.js provides exactly that.

The talk was given by Juriy "kangax" Zaytsev on Fabric.js and you can check out the demos from here:
http://kangax.github.com/fabric.js/test/demo/

JavaScript JIT

Zbigniew Braniecki of Mozilla gave a really interesting talk about JavaScript engine Just-in-time compilation methods. There are two types of compilation, method and tracing. Here's how I understand they work. Method counts the number of times a method is called and if it reaches a certain number of calls then the method is compiled (no longer interpreted). Tracing analysis code paths and compiles code paths that are commonly executed. Apparently there is very little documentation on this and Zbigniew had to do a lot of work to find out which methods each browser uses and to come up with the slides.

The point of his talk was that we can actually help compilers make our apps faster by writing code with the compilers in mind. In most cases this won't be necessary but if you are trying to squeeze that extra bit of performance out of your app it's something you might consider.

Slides: http://www.slideshare.net/zbraniecki/js-compilation-falsy-values-slides

PhoneGap

Unfortunately I missed this one but it was delivered by Brian LeRoux and he demonstrated how to build an app in PhoneGap. Go check out PhoneGap, it sounds super useful.

Has JavaScript won?

There were two other presentations, first Tom Hughes-Croucher on node.js, who also ran the node.js workshop on the first two days, and finally Tantek Çelik talking about his new project CASSIS which defines a sub-set of JavaScript which can run both in JavaScript and PHP. As I mention in my post about Falsy Values on the Pusher blog, right now is a great time to be a JavaScript developer. You can now developer server applications, in the browser, for mobile devices and also for the desktop.

It's really strange to think that in 2004 I left a job because I was doing purely JavaScript and I wasn't sure the language was going anywhere. How wrong was I? :)

Finally, a big thanks to Pawe? Czerski and Damian Wielgosik who organised Falsy Values. Well done guys!