views:

675

answers:

12

When looking through some code that was handled by another employee, I see a lot of code written in:

do{
    ...
}while(false);

What advantage (if any) does this provide?

Here is more of a skeleton that is happening in the code:

try{
    do{
        // Set some variables

        for(...) {
            if(...) break;
            // Do some more stuff
            if(...) break;
            // Do some more stuff
        }
    }while(false);
}catch(Exception e) { 
    // Exception handling 
}

Update:

C++ Version:
Are do-while-false loops common?

+12  A: 

No advantage. Don't do it.

Michael Myers
It has the advantage of enraging mmyers. Isn't that reason enough to do it?
Pesto
I'm not enraged. Then again, I don't have to maintain this guy's code.
Michael Myers
you *should* be enraged. Didn't you see his while loops! ;)
Mitch Wheat
+2  A: 

Your intuition is right. That is a completely useless thing to do.

It is possible that whoever coded it originally had something other than false as the condition, and simply changed it to false rather than removing the entire block not to lose this "history" of the code. This is a just clutching at straws however. To be quite frank it's just a plain example of a tautology, which has no place in code.

Noldorin
Woah, what's with the sudden down-votes? It is courtesy to leave a comment when you down-vote, and the reason has not already been explained!
Noldorin
+1 to get your post upwards again. However, i can imagine some people are confused by your analogy with "logical tautology" and think that you talk about "false" being a tautology. Can you please elaborate on that analogy?
Johannes Schaub - litb
@litb: Cheers. And yeah, I used the wrong sort of tautology there perhaps. It is certainly a tautology in the common sense of the word: the fact that you write the code once, then add `do { ... } while (false)` again (unnecessarily) specifies that it should be added once.
Noldorin
A: 

I'm going to guess the author of that didn't trust his code so he used to run it a couple times to see if that made it work more and this is the archeological remains of that.

Ben S
A: 

The only reason I can think of is to create a block to make variables declared within the {...} more tightly scoped, but there are better ways of doing this (like creating functions, or just creating blocks - hat tip to Pete Kirkham).

Dominic Rodger
Or just create a block.
Pete Kirkham
Yeah, though personally I find random blocks in code an eyesore.
Dominic Rodger
Surely `do ... while (false)` are not just an eyesore but a brainsore too?
Andrzej Doyle
@dtsazza - Agreed. I'm not suggesting this is a good way to code, just that it might be what the developer was doing originally, though I confess I find Martin's suggestion more compelling.
Dominic Rodger
+1  A: 

In pretty much every language other than C/C++ this provides no tactical advantage.

In C/C++ there is a case with macros where do/while(false) makes it easier to expand a macro safely into multiple statements. This is advantageous when the macro otherwise looks like a normal function call.

JaredPar
+11  A: 

Maybe it was done to be able to jump out of the "loop" at any time, e.g:

do
{
    ...
    if (somethingIsWrong) break;
    //more code
    ...
}
while(false);

But as others have said, I wouldn't do it like this.

M4N
That's a good point!
Moayad Mardini
Yeah, let's hack around the fact that our language doesn't have `goto`!
Michael Myers
I considered this, but given that a simple `if` statement (or horror, a `goto`) would do the job much more simply, I doubt it somewhat.
Noldorin
Wow - it's a Java-style goto! (but it's begging to be a function with a conditional return instead...)
moonshadow
@mmyers: it more or less the same as (ab-)using exceptions for flow control
M4N
Although you don't need it in Java as you can label the loop.
Tom Hawtin - tackline
I'm fairly sure that the only way to make this more convoluted is to use a try {} finally {} block somewhere in between. This code makes me sad.
Malaxeur
moonshadow: Why would abusing return be any better? You would be splitting the existing method at possibly a very awkward point.
Tom Hawtin - tackline
Noldorin: If there are multiple `if`-`breaks`, the equivalent `if` tree would be disappearing off the right hand side of the screen.
Tom Hawtin - tackline
@Tom, because you *know* in what method you are when you see that *return*. But if you see a "break", you usually think "oh, that's breaking out of the loop - wait, LOOP??", it is just confusing i think.
Johannes Schaub - litb
In the OPs question the break is breaking out of an inner for loop and not the do while loop.
pjp
Java **has** `goto`
fortran
@fortran: Yes, for values of "has" equaling "will recognize and give a compile error if you use".
Michael Myers
@mmeyers I see that this is the current behaviour, but I'd swear that long, long time ago, when I had my first programming classes at the university (circa 2000), a fellow student surprised the teachers using `goto` in a Java assignment xD... I have to research if early Java 1.1 really supported that or I am being tricked by my memory.
fortran
+2  A: 

This is used in C to define block inside macro. See this for example.

Example, the following is invalid:

#define f(x) { g(x); h(x); }

if (x >= 0) f(x); else f(-x);

but with this definition, it will work:

#define f(x) do { g(x); h(x) } while(false)
alex
Java doesn't have macros, and if it did having a semicolon following a closing brace is legal in Java, so it wouldn't be needed.
Pete Kirkham
Having semicolon is legal unless you want to put 'else' there.if () {}; else {} - is illegal
alex
You might want to expand on that.
Tom Hawtin - tackline
This seems like the most logical explanation, and backs up @moonshadow.
ericp
+1  A: 

Have a look at this question

The OP asks about this exact construct and explains some reasons for using it. The consensus seems to be that it's a bad thing to do.

Glen
+10  A: 

In Java, there is no reason to do this.

In C, this is a common idiom when defining macros:

Consider:

#define macro1 doStuff(); doOtherStuff()
#define macro2 do{ doStuff(); doOtherStuff(); } while( false )

if( something ) macro1; // only the first statement in macro1 is executed conditionally
if( something ) macro2; // does what it looks like it does

...but macros in C are evil and should be avoided if at all possible.

Does your coworker come from a C background?

moonshadow
+1 interesting.
RJFalconer
+1  A: 

It is useless, but the coder could have used a multiple break commands to do some weird exception handling.

do{
if(<something>){...}else{break}
if(<something else>){...}else{break}
...
}while(false)

Granted its stupid, but I did find something like in a old c program once

Rodrigo
+1  A: 

As a placeholder for a future when some other condition is put in place of false.

mobrule
+1  A: 

It could be used to skip execution of some code (like a goto or something) but when I look at it again, there seems to be a for loop (where the if(...) break; statements are) in the do-while. Otherwise, I would say that it would be a Java version of a goto...

Reakus Rogue