Jump to content

  • Log In with Google      Sign In   
  • Create Account

Calling all IT Pros from Canada and Australia.. we need your help! Support our site by taking a quick sponsored surveyand win a chance at a $50 Amazon gift card. Click here to get started!

Unique Style of Flow Control

Posted by Jason Z, 08 June 2014 · 173 views

I recently picked up a copy of the book "Developing Microsoft Media Foundation Applications" by Anton Polinger. I have been interested in adding some video capture and playback for my rendering framework, and finally got a chance to get started on the book.

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.

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.

I have asked around, and this seems to be at least a known practice. If there are any interesting use cases where this makes especially good sense, I would love to hear about them!

This is just a way of hiding the fact that you're essentially using "goto". If that's what you really want, man up and just use "goto ErrorHandling;" I've seen similar styles of using for(ONCE) { }, with the idea that you use break to jump to the end of the block. Once again, it's just a disguised goto.

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.

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.




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++.



August 2015 »

2324252627 28 29

Recent Comments

Recent Comments