What I immediately found interesting was in the foreword on page xvi, the author describes a coding practice that he uses throughout the examples. The idea is to use a 'do {} while(false)' pattern, and he puts all of his operations into the brackets of the do statement. The loop will execute precisely once, and he wraps all of those operations with a macro that 'break' on an error. This effectively jumps to the end of the block when an error occurs, without requiring verbose return codes or exception handling.
What I immediately found interesting was in the foreword on page xvi, the author describes a coding practice that he uses throughout the examples. The idea is to use a 'do {} while(false)' pattern, and he puts all of his operations into the brackets of the do statement. The loop will execute precisely once, and he wraps all of those operations with a macro that 'break' on an error. This effectively jumps to the end of the block when an error occurs, without requiring verbose return codes or exception handling.
[color=rgb(0,0,0)][background=transparent]I haven't ever seen this type of flow control, so I was wondering what your thoughts on this are. I would assume that the compiler would optimize out the loop altogether (eliminating the loop, but still executing the block) due to the always false condition, but the macros for checking failure would still end up jumping properly to the end of the block. It seems like a relatively efficient pattern for executing a number of heavy duty operations, while still retaining pretty good readability.[/background][/color]
[/font]Comments
I've seen the do { } while(false) pattern a few times, mainly to provide scopes to C macro-expanded code (no idea why they couldn't just write { } ). I didn't know about the break trick, I'm not sold on it.
I use this pattern sometimes on initialization function, but instead of do while, I use while(1) { ... ... .. break; }
Yeah, this is absolutely what functions are for. This suffers from the same issues that goto suffers from, except its slightly more explicit, but I fail to see the advantages.
Thanks for the comments everyone. The general consensus that I have seen (both here and elsewhere) is that a few people like and use this construct in limited circumstances, while the majority of people either haven't seen it or don't like it.
In any case, I wanted to post it here, so that people can make their own decision about their own styles. I think the only real benefit of this is readability, at the expense of using a strange macro driven construct. For modern code I would tend to stay away from this one and just use exception handling instead.
Thanks again for the comments and experiences!
I've seen the do { } while(false) pattern a few times, mainly to provide scopes to C macro-expanded code (no idea why they couldn't just write { } ). I didn't know about the break trick, I'm not sold on it.
http://bytes.com/topic/c/answers/219859-do-while-0-macro-substitutions
I think the key reply in that conversation, is
The whole idea of using 'do/while' version is to make a macro which will
expand into a regular statement, not into a compound statement. This is
done in order to make the use of function-style macros uniform with the
use of ordinary functions in all contexts.
Regarding the while(true){...break} thing, I pretty much agree with what everyone else here has said. It is essentially a poor-mans try-catch block. If you can't use exceptions and you have a lot of shared state (making the encapsulation of the failure prone code into functions undesirable), a structured goto can be a clean way of handling and cleaning up from a failure. I have mostly seen that in the context of hardward initialization (so often it is C anyway). However, I would default to exceptions and encapsulation (functions or classes where appropriate) when working in C++.
-Josh
If you put your "failable" code into a function, then you can use return and have the calling function have the error handling / cleanup code. As coding patterns go, this is much less likely to confuse people.