Unique Style of Flow Control

Published June 09, 2014
Advertisement
[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]

[/font]
2 likes 6 comments

Comments

Mike.Popoloski
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.
June 09, 2014 02:30 AM
Krohm

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.

June 09, 2014 05:04 AM
Giallanon

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

June 09, 2014 08:07 AM
Aardvajk

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.

June 09, 2014 09:13 AM
Jason Z

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!

June 09, 2014 10:10 PM
jjd

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

June 10, 2014 11:35 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement