ninja_coder (
ninja_coder) wrote2008-07-18 02:00 pm
![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
Entry tags:
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.)
no subject
Which brings up an interesting question: How does Ruby handle the one line if?
no subject
Shortcut programming is teh suck.
no subject
if (condition);
do_something();
See, you'll always
do_something
, because(condition)
only applies to the statement immediately afterwards ... and that statement ends with the first semicolon. But semicolons are so common in Java it's very easy for the mind to overlook the fact that the first one there is spurious, and what you wanted to write wasif (condition)
do_something();
(and as you point out, what you really wanted to write was
if (condition) {
do_something();
}
no subject
if(condition); {
do_something();
}
and there the braces don't really help you, alas.