ninja_coder: (Default)
ninja_coder ([personal profile] ninja_coder) wrote2008-07-18 02:00 pm

The Usefulness of Nunchaku (Now, If Only I Had a Pair...)

 
Wow, I think this is the first time I've spent a half-hour laboriously battling an enemy, and then at the end thought, "That problem would never have happened if I were using the nunchaku."

Perhaps a little explanation is in order: I don't actually use the nunchaku! But I have studied it a tiny bit, and I have realized that, while one of its most well-known (and strangest) features makes many warriors wary of it. But not me. I see that feature as a useful thing, even if it's slightly different from how most things work.


In particular, I'm talking about whitespace-sensitivity. Even more pointedly, I'm talking about the fact that many languages with C-like syntax (including JavaScript) allow you to dispense with curly braces around an if() block if there's only one line. For example:

if (alpha == beta) {
     do_something();
}


can just as legally be written as:

if (alpha == beta)
     do_something();


Kernighan and Pike, in their book The Practice of Programming, point out that using this syntax is a really bad idea. What happens when you add a few lines to this conditional? You read:

if (alpha == beta)
     do_something();
     frob_the_frammistat();
     degauss_the_modem();


and, like a silly human, you think it's only going to do all those three things if alpha equals beta. But the JavaScript interpreter, which cares more about the rules of the language than it does about whitespace, sees this:

if (alpha == beta) {
     do_something();
}
frob_the_frammistat();
degauss_the_modem();


Then you sit there, trying all sorts of things, wondering why the frammistat keeps getting frobbed, even though alpha is clearly not equal to beta...

This would never happen in Python. In Python, the way the code looks like it's indented is, perforce, how it's actually interpreted.

I would like to point out that the code I'm dealing with, which foolishly omits those curly braces, is not my own. This sort of thing is why (following Kernighan and Pike's sage advice) I don't usually leave those things off.


The way the nunchaku are built, even if it's a bit off-putting to many warriors, is actually a great strength, that would never have allowed that particular enemy to ambush me that way. (Shō, my friend, I can hear you laughing already. I share your opinion.)

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting