On Sun, Jan 16, 2011 at 2:38 AM, Ryan Grove <r...@wonko.com> wrote:
> On Sat, Jan 15, 2011 at 5:15 PM, And Clover <and-...@doxdesk.com> wrote:
>> If you really want to make your code idiot-resistant, you still don't
>> have to move loop `var`s all the way to the top of function. This:
>>
>>    function doSomething(things) {
>>        // Loop that does something
>>        var i, thing;
>>        for (i= 0; i<things.length; i++) {
>>            thing= things[i];
>>            thing.doPartOfSomething();
>>        }
>>
>>        // Loop that does something else
>>        var i, thing;
>>        for (i= 0; i<things.length; i++) {
>>            thing= things[i];
>>            doAnotherPartOfSomething(thing);
>>        }
>>    }
>>
>> is explicit and still keeps the advantage of locality for associated
>> declarations.
>
> This is still a bad idea.
>
> Thanks to hoisting, that second declaration doesn't do what it appears
> to do (at least, not where it appears to do it), and it increases the
> likelihood that a less experienced programmer (or just someone who
> isn't paying close attention) will make a mistake when changing that
> code later.
>
> If another developer comes along and adds some code after the second
> var statement but before the second loop that assumes that i and/or
> thing are undefined, they'll be in for a surprise, because i ===
> things.length and thing === things[things.length - 1].
>
> - Ryan
>
> --
> To view archived discussions from the original JSMentors Mailman list: 
> http://www.mail-archive.com/jsmentors@jsmentors.com/
>
> To search via a non-Google archive, visit here: 
> http://www.mail-archive.com/jsmentors@googlegroups.com/
>
> To unsubscribe from this group, send email to
> jsmentors+unsubscr...@googlegroups.com
>

Correct Ryan, never redeclare twice the same variable in the same
block and always move declaration atop the respective functions body.

This shows one more reason why people shouldn't declare variables
inside a loop block:

    for (var i= 0; 10 > i; ++i) {
        var a;
        if (i == 5) a = true;
        alert(a);
    }

in this particular case the "a" variable is initialized to "undefined"
only once at the beginning, in fact the parser will internally convert
this code to:

    var a;
    for (var i= 0; 10 > i; ++i) {
        if (i == 5) a = true;
        alert(a);
    }

so this is the reason I always try to have only one "var" keyword atop
my functions where I group all the declarations (like the parser
does).

By doing this I believe the next guy reading and/or modifying my code
is not tricked in to think the "a" variable is redefined as
"undefined" at each iteration.

--
Diego

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/jsmentors@jsmentors.com/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/jsmentors@googlegroups.com/

To unsubscribe from this group, send email to
jsmentors+unsubscr...@googlegroups.com

Reply via email to