do/while false

A

Angus Graham

Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;


This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading. The same could be
achieved with a boolean flag.

e.g.,

bool bSuccess = CatchFish(pFish);
if (bSuccess) {
bSuccess = CleanFish(pFish);
}
etc.

Exceptions are not being used because we are assuming that the methods
can return false under non-exceptional circumstances.

Any comments on this sort of do/while (false) style?

Angus.
 
A

Aggro

Angus said:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;

What's wrong with if - else if?

if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}
delete pFish;
 
D

David Harmon

This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading. The same could be

Well, you could do it Perl style...

CatchFish(pFish)
&& CleanFish(pFish)
&& CookFish(pFish)
&& EatFish(pFish);
 
A

AngleWyrm

This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading.

The advantage of a do loop is that it is guaranteed to execute at least
once; but to my mind they imply the possibility of executing more than once.

It reminds me of two other questionable but popular language structures: the
for(;;) loop and the if(true) statement

I avoid their use as well.
 
U

Unforgiven

Final if() is redundant.
What's wrong with if - else if?

if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}

Here too.
delete pFish;

Or if you want to avoid the empty if statements, at the price of some
nesting:

if( CatchFish(pFish) )
if( CleanFish(pFish) )
if( CookFish(pFish) )
EatFish();
delete pFish;
 
D

Denis Perelyubskiy

Angus Graham, 2/14/2004 9:46 AM:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;
[...snip...]

I think some people use this sort of thing (perhaps not in as simple a
context as this) as a substitute for 'goto'.

denis
 
A

Alf P. Steinbach

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;

Well, they don't know shit about programming. Should have used a
std::auto_ptr here. That code is not exception-safe.


This is effective but seems to me to be bad style.
Yep.



The do/while loop
isn't really a loop at all which is misleading. The same could be
achieved with a boolean flag.

e.g.,

bool bSuccess = CatchFish(pFish);
if (bSuccess) {
bSuccess = CleanFish(pFish);
}

Hungarian notation is [censored derogatory expression].

And succcess/failure flags are seldom useful for anything.

Use a simple if-else.


Any comments on this sort of do/while (false) style?

The do-while style is useful in macros, allowing a macro to be used
as a statement in all cases where C/C++ allows a statement.
 
J

John Kewley

...
Well, they don't know s*!t about programming. Should have used a

Why use foul language here, and then censor yourself when
referring to Hungarian notation?
Hungarian notation is [censored derogatory expression].

And succcess/failure flags are seldom useful for anything.

Very useful in making sense of a more complicated if then else where
it is harder to ensure a single "else" part.
Use a simple if-else.

Agreed, this would make most sense.

I think it was the mid 1960s when goto-less programs were championed, this
seems to be a throwback to then :)
The do-while style is useful in macros, allowing a macro to be used
as a statement in all cases where C/C++ allows a statement.

Agreed - an excellent use of "do ... while (0);" (assuming you have
suitable
justification for using macros in the 1st place :)

JK
 
A

Alf P. Steinbach

Why use foul language here, and then censor yourself when
referring to Hungarian notation?

I thought what I was about to write about Hungarian notation would
qualify as foul language... ;-)
 
M

MPBroida

Angus said:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;

This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading. The same could be
achieved with a boolean flag.

Could be a placeholder for LATER development when that
chunk would need to be a real loop. When they get to
that point, they can replace the "false" with whatever
condition is applicable. This gives them a quick one-
time run through the code without having to drastically
change the syntax to add looping later.

Mike
 
O

Old Wolf

I've recently come across a number of programmers who use the following
What's wrong with if - else if?

if (! CatchFish(pFish)) {}
else if (! CleanFish(pFish)) {}
else if (! CookFish(pFish)) {}
else if (! EatFish(pFish)) {}
delete pFish;

This form doesn't let you insert extra statements without obfuscation;
eg. if you wanted to perform some series of non-checked actions
in between the cleaning and the cooking

To the OP: using try...catch is a bit less verbose,
especially if you want to know what the failure reason was at
the end of the code.
 
D

David Rubin

Angus said:
Hi:

I've recently come across a number of programmers who use the following
type of do/while(false) structure quite often:

do {
if (! CatchFish(pFish)) {
break;
}
if (! CleanFish(pFish)) {
break;
}
if (! CookFish(pFish)) {
break;
}
if (! EatFish(pFish)) {
break;
}
while (false);
delete pFish;


This is effective but seems to me to be bad style. The do/while loop
isn't really a loop at all which is misleading.

This technique is typically used in definitions of debugging macros to
introduce a scope for local variables. For example (admittedly more of a
C example than a C++ example):

#define LOOP(f,x,a,b) do{ int i, j; \
for(i=0; i<a; ++i){ \
for(j=0; j<b; ++j){ \
printf("debug: " f, i, j, x[j]); \
} \
} \
}while(0)

which can be used as

int x[10][20] = {...};
DBG("x[%d][%d]=%d", x, nelem(x), nelem(x[0]));

I agree that I would not recommend its use as you have presented it.

/david
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top