Thursday, September 27, 2007

An endless rebuttal

In response to this travesty of an argument...

a while loop is really a syntactic sugar for the more general for loop in which a condition is expected to be false at some time during its execution.
Who says a condition is "expected to be false"? Maybe we're writing server code and the loop really is intended to continue forever.

for (;;) {
}
It simply and clearly states that a loop will continue to iterate until some internal condition dictates that it is now time to break out.
That's poppycock. How many steps in translation does it take to get from "for(;;)" to "loop forever". Once you've learned that "for" means "loop", then you've got to figure out that "(;;)" means "forever". I know that seems pretty simple to all you veteran programmers out there, but I'd prefer to save brain-space for something more useful. The "while(true)" construct doesn't compel me to maintain two definitions for "while" in my brainspace (I speak english natively). While means while and true means true. Granted, we could extend the syntax with prepositions. But no semicolons, that's for sure.

I know what you're thinking: "English isn't C and C isn't English. If you're overlapping languages in this way, you're bound to mess something up." I agree. I'm just saying that there's no sense in creating new syntax if the old syntax is just as effective at accomplishing the same task. In this case, while(true), English is just as effective. (When I say effective, I mean "well-designed").

Finally, I think we could both agree that any language would be better served with a "loop" construct that doesn't require any condition for execution. My opinion is that the following construction is preferable to either while(true), or (the ghastly) for (;;):

loop
{
    do_something();
    if (something_happened())
    {
        break;
    }
}

I've learned from sad experience that the order of execution in a loop can make a big difference when it comes to the correctness of the code. Because the exit condition is explicitly located relative to the processing, there's no ambiguity.

0 comments: