Lurking bugs

Cockroach

Thou Shalt var Thy Locals.

Let’s say you use behaviour.js or modifiedbehavior.js. Both currently contain for-loops with non-local loop variables, i.e. loop variables not declared with var:

for (i=0;element=list[i];i++){...

Let’s say you are no better, and you define a behaviour with the same blooper, a var-less for-loop, with the same variable name.

That’s right, your loop now shares a loop variable with another, enclosing loop in a library somewhere. Have fun hunting down your bug!

In my case it manifested itself as an infinite loop that hung Internet Explorer. In your case, you may well get a different manifestation. Neglecting to var your local variables invites all kinds of trouble.

The library bug only bit me because I made the same mistake myself. Treat your own locals right; spare yourself the troubleshooting and the forehead-slapping “d’oh!” moment.

Comments are closed.