The case against do-while

Posted by    |       Ceylon

So the comment thread of my previous post got me thinking again about the do/while statement. Frankly, it's difficult to see why we really need this as a first-class construct in modern programming languages. Here's my list of reasons for saying that.

First, at least in C-like languages, the syntax is pretty irregular. It's the only control statement that doesn't follow this syntax pattern:

( keyword ("(" Something ")")? Block )+

Instead its syntax features two glorious keywords and a stray - totally extraneous - semicolon. Weird.

('Cos, like, even with the help of two keywords and a pair of parens, the parser still needs that extra juicy semicolon to know when to stop looking for more bits of the do/while statement. Sure, that makes sense.)

Anyway, I figure that it's this irregularity that apparently leads different people to have different intuitions about the scope of declarations contained in the body of the do/while.

Second, do/while demands that we reserve as a keyword one of the most useful verbs in the English language. I could have found all kinds of uses for the word do if Java would just let me use it as an identifier!

Third, do/while is easily emulated using a while loop. The following:

do {
    something();
}
while (!finished);

is only somewhat less readable when written like this:

while (true) {
    something();
    if (finished) break;
}

And this second formulation doesn't open up any debate about the scope of things.

Fourth, the current crop of programming languages all have support for higher-order functions, allowing libraries to introduce new kinds of flow control, and taking pressure off the language itself to provide every possible kind of loop baked into its basic syntax.

Finally, even though I've written a reasonably significant amount of Java code in over more than a decade of familiarity with the language, I can barely recall ever having found a use for this construct. Unless my memory is even worse than I think it is, I can't possibly have used do/while more than two or three times in my entire career!

So I'm strongly considering simply dropping do/while from Ceylon. It's simply not pulling its weight.


Back to top