I actually don’t hate JavaScript. I’m typically not a fan of duck-typed languages, and JavaScript has caused me no small number of headaches, but it has its uses, and it beats the alternative. I could be writing in VBScript, after all…
Still, there are a few things about JavaScript that really, really tick me off. One of those just bit me badly this morning. Take a look at the following block of code:
1: //Recursively sets the nodes and their children to the
2: //specified checked state.
3: function UpdateChildren(nodes, checked)
4: {
5: for (i=0; i < nodes.get_count(); i++)
6: {
7: var node = nodes.getNode(i);
8:
9: node.set_checked(checked);
10:
11: if (node.get_nodes().get_count() > 0)
12: UpdateChildren(node.get_nodes(), checked);
13: }
14: }
See anything wrong with that? Seriously, stop and look at it before continuing any further. I stared at that code for about an hour before I finally figured out what was wrong.
The code looks pretty innocuous. It’s a recursive method that walks a tree, basic stuff. The problem though, is that the recursion was never ending. It became stuck in a cycle where it was repeating the same recursive calls over and over, and I couldn’t figure out why. It looked like the recursion was "leaking" somehow. Turns out, that’s actually what was happening, but it wasn’t JavaScript’s fault. I left the ‘var’ keyword out of the ‘i=0’ assignment. That means that ‘i’ is created a global variable instead of a local variable!
I would *much* prefer that JavaScript have given me an error when I tried to assign a value to ‘i’ than just saying "meh, I’m sure you meant that to global." Whoever thought that was a good idea obviously hates humanity and wanted to spread agony and sadness. <sighs>, oh well, back to work.
NOTE: there won’t be an installment of "how to run a software company (into the ground) this week because I am running way behind. Sorry!