Monday 14 August 2006

JSLint

I thought I have written about this cool tool long time ago. However, when I tried to search for it, I could not find it. So, not searchable means "I have not written about it yet". Here we go.

JSLint, a tool written by Douglas Crockford, has saved a lot of my time as I develop the client-side functions of Fablusi. It checks for common Javascript coding errors and warns a number of "tolerated" practices.

Dean Edwards points out that JSLint is not a silver bullet in JSLint Considered Harmful which I will respectfully disagree.

I don't like the mandatory braces on if statements. I've become rather fond of one-liners like this:
function foo( x ) {
if( ! x ) return;
// now process x
}


This one-liner will create error when you want to compact the javascript by removing the white spaces!

JSLint's prohibition against repeated "var" declarations can lead to fragile code:
function foo() {
for( var i = 0; i < a.length; i++ ) {
}
// and later in the function:
for( i = 0; i < b.length; i++ ) {
}
}

If you later refactor that code, it would be easy to forget to put the "var" back in.


I would rewrite the code like this:
function foo() {
var i=0;
for( i = 0; i < a.length; i++ ) {
}
// and later in the function:
for( i = 0; i < b.length; i++ ) {
}
}

This will not generate a warning by JSLint and it is a good practice! Re: refactoring - we should put the var in anyway! JSLint will warn you if you have forgotten!

As pointed out by Dominic Mitchell is his post JSLint Backlash, ‘Considered Harmful’ Essays Considered Harmful.

No comments: