Hi Michael, I'm so sorry for the confusion that I made!
I don't know why, but I thought you were talking about the jQuery rails.js driver. I've just seen you are talking about the default Prototype driver when you talked about the handleMethod function, which I didn't find in the jQuery driver:
http://github.com/rails/jquery-ujs/blob/master/src/rails.js
But now, I think you're probably right on the repeated declarations.
Regarding Crockford code writing rules, I've heard about him but have never been interested on reading his tips at all... I don't like the idea of following some rules without understanding the reasoning. Since I never had problem in organizing my js, I didn't find the need for looking for some guides on this topic.
For example, once you know the caveats of not using the semi-colons, I don't see any problems in using that feature. Actually, I dislike using semi-colons since the code is cleaner for my taste when not using semi-colons, unless you have multiple statements in the same line. Yes this can make sense, for instance:
var r=; for (var i in ary) r.push(ary[i]*2)
I also don't like the idea of using some tool for formatting code. For instance, the above code would be indented in Netbeans, probably as:
var r=;
for (var i in ary) {
r.push(ary[i]*2);
}
Some people may argument that this is much cleaner/better than it was. I don't think so. Why? Because I think that some simple idea should read as a single statement. 4 lines for doing that little job is too much for me. The problem is that some scripts may have thousands of lines. If you waste space like the code above, it becomes difficult to read other's code and get the idea without having to scroll lots of pages to finish reading some function, for instance.
Another common code writing mistake I see on people's code:
function x() {
if (! exception_condition) {
// several lines of code
}
}
This is much better written as:
function x() {
if (exception_condition) return;
// same lines of code without unnecessary nested statements
}
This is much better because you don't need to be worried about what happens if exception_condition occurs. And deep nesting is always more difficult to read.
About defining all variables at the beggining of some function is not a great thing at my opinion. It is much better to have a fluid source code where you are only concerned about some variable when you are reading the related code. Also, I always avoid declaring variables unnecessarily. That is because there is no standard way for giving it a scope. For instance, when you are reading code like this:
function x() {
var x = ... // something meaninful
// we are using the value of x here
// ...
// here follow several other lines where x is no more relevant
// how can the reader know that? He will be worried what else can be affected if he refactors the above x related code.
}
There are several situations where variable declarations are unnecessary. For instance, using jQuery:
var el = $('<div/>')
var body = $('body')
body.append(el)
This could be written using no variable as:
$('<div/>').appendTo('body')
Or even as:
$('<div/>').text('content').appendTo('body').click(function(){alert('clicked: ' + $(this).text())})
or
$('<div>content</div>').click(function(){alert('clicked: ' + $(this).text())}).appendTo('body')
I usually use this approach whenever possible. I think it is much clear for other developers trying to understand what the code does. When there is some declared variable, you have no idea when and what it will be used for.
Also, I don't like to see 80 characters only page delimiter because most screens nowaday are wide and have small height. This mean we should avoid several lines of code in favor of lengthy lines. What I mean is that there was some reasoning for using 80 characters delimiter in the past that is no more valid nowadays. It is much better to get as much code as possible without the need of scrolling, unless the code readability is affected, which I don't think is the case in the above examples.
I don't see any problems either with the ++ and -- operators, but of course someone should understand how they work. And Javascript should be tested as well, the language is not an exception. There are good frameworks for that, as Selenium, for instance. So, why avoid the great features of the language? These operators works exactly the same way as C, C++ and Java...
But I hardly use them anyway. They are most used in for loops. I barely use constructions like "for (var i=0;i<10;i++)". When working with jQuery, I usually use something like $.each() and if I am using pure Javascript, usually a for loop is used in some context where the syntax "for (var el in elements)" is more suited.
Well, actually I was wondering writing some post about this topic on my site, but ended up never writing about it. Maybe, now that I have already written part of the content in this email, I can put this on my site I always wanted to write about this for some years now whenever I have to read other's code in my company
Here I have only given JS examples, but there are other subjects in other languages as well I would like to cover...
Sorry for the post-in-email-shape Too long for an e-mail, probably, but please don't blame me. You have inspired me with this subject
Thanks for reading until here,
Regards,
Rodrigo.