Jump to content
  • Advertisement
  • entries
  • comments
  • views

Unique Style of Flow Control

Sign in to follow this  
Jason Z


[font=arial]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.

[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]

[color=rgb(0,0,0)][background=transparent]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![/background][/color]

Sign in to follow this  


Recommended Comments

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.

Share this comment

Link to comment

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.

Share this comment

Link to comment

I use this pattern sometimes on initialization function, but instead of do while, I use while(1) { ... ... ..  break; }

Share this comment

Link to comment

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.

Share this comment

Link to comment

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!

Share this comment

Link to comment

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



Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!