Click here to Skip to main content
15,918,808 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 15:43
mvePIEBALDconsult10-Dec-08 15:43 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
FatBuddha11-Dec-08 3:04
FatBuddha11-Dec-08 3:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult11-Dec-08 6:14
mvePIEBALDconsult11-Dec-08 6:14 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
supercat915-Dec-08 11:57
supercat915-Dec-08 11:57 
GeneralRe: Avoid return statement in the middle - horror or not? [modified] Pin
PIEBALDconsult15-Dec-08 13:33
mvePIEBALDconsult15-Dec-08 13:33 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
supercat916-Dec-08 8:14
supercat916-Dec-08 8:14 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult16-Dec-08 13:13
mvePIEBALDconsult16-Dec-08 13:13 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
supercat916-Dec-08 17:20
supercat916-Dec-08 17:20 
PIEBALDconsult wrote:
Where do you declare it?


I usually have a module with any global variables that are required by my macros. Not wonderful, but I don't know any better approach in C.

PIEBALDconsult wrote:
I've only seen that in the GCC documentation where it talks about "swallowing the semi-colon".
I've certainly never seen it in production code, and I hope I never do.


When trying to write a macro that behaves like a void function but contains an 'if' statement, something like:
#define foo(x) do {if (x > 0) do_this();} while(0)
is clearer than:
#define foo(x) if (x > 0) do_this(); else
while nonetheless avoiding the difficulties associated with:
#define foo(x) {if (x > 0) do_this();}
or worse, the bug-prone
#define foo(x) if (x > 0) do_this()
which makes me cringe.

PIEBALDconsult wrote:
And I don't write a loop that isn't a loop.


What would be your preferred way of allowing a procedure to be done in multiple steps and abort at any step, while ensuring that any necessary cleanup would be completed? If you prefer a "goto" label to a do/while(0) structure with break statements, fine. That's actually my usual preference. Can you suggest anything better than the do/while(0) for people who dislike goto?
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult17-Dec-08 12:24
mvePIEBALDconsult17-Dec-08 12:24 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Lutosław12-Dec-08 10:11
Lutosław12-Dec-08 10:11 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 10:45
mvePIEBALDconsult12-Dec-08 10:45 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
rfidel10-Dec-08 5:03
rfidel10-Dec-08 5:03 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult10-Dec-08 8:33
mvePIEBALDconsult10-Dec-08 8:33 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Manish K. Agarwal10-Dec-08 21:32
Manish K. Agarwal10-Dec-08 21:32 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult11-Dec-08 6:15
mvePIEBALDconsult11-Dec-08 6:15 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Thomas Weller11-Dec-08 19:56
Thomas Weller11-Dec-08 19:56 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 3:18
mvePIEBALDconsult12-Dec-08 3:18 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Marcello Faga11-Dec-08 1:30
Marcello Faga11-Dec-08 1:30 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
riced11-Dec-08 14:04
riced11-Dec-08 14:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Thomas Weller11-Dec-08 19:49
Thomas Weller11-Dec-08 19:49 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult12-Dec-08 3:34
mvePIEBALDconsult12-Dec-08 3:34 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
Rob Grainger16-Dec-08 3:10
Rob Grainger16-Dec-08 3:10 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
coolM18-Dec-08 8:04
coolM18-Dec-08 8:04 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
PIEBALDconsult18-Dec-08 8:41
mvePIEBALDconsult18-Dec-08 8:41 
GeneralRe: Avoid return statement in the middle - horror or not? Pin
coolM18-Dec-08 10:57
coolM18-Dec-08 10:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.