36

Today at work while reading legacy code:

do {
...
} while(false)

Wanna cry and laugh at the same time

Comments
  • 9
    https://stackoverflow.com/questions...

    WELCOME TO LEGACY CODE SON. :P

    The other possibility is, if you're looking at code that was generated by some other system and the "false" is programmatically generated one level up. (Think embedding server-side variables in some JS code.) It's not pretty, it's not nice, but I really doubt the developer was under the impression that this block of code would run more than once.
  • 1
    I use this nearly every day. Good for cutting down on brackets or avoiding using nested ifs as you can just break; out of the loop if eg. a validation condition fails.
  • 3
    I use that a lot for multi-line macros because that doesn't fuck up when you use that macro after e.g. an if without extra curly braces.
  • 0
    @HollowKitty i laughed...i looked...i felt bad....the break how can one miss that ....but using try catch with custom exception wouldn't do it? Just wondering
  • 0
    @v1shky C doesn't have try/catch. It does have setjmp/longjmp, but while/break is easier to read.
  • 1
    @Fast-Nop your username scared me why is someone being rude 😂...then sure someone came up with this must be a genius.
  • 2
    @v1shky Actually, I wanted "Nop" as it's my favourite assembly instruction - works everywhere. :-) But three letters are not allowed, so I took the accelerated version instead which doesn't even need memory. ^^
  • 0
    @Fast-Nop smart and what about nohup 😂
  • 1
    @v1shky doesn't exist in bare metal dev. ^^
  • 1
    @Fast-Nop sorry me noob ✋
  • 2
    @Fast-Nop I am generally a “use curly braces everywhere” dev; so the reason why these type of macros existed always mystified me. Makes so much sense now. It’s the only safe way to define a (multi-statement) macro that is to behave syntactically as a function, no other precompiler solution in c provides this. Thanks for the insight.
  • 1
    @nemyxa "don't cry for me I'm already dead code;"
  • 0
    @HollowKitty Thanks for the SO link. As another post said, learnt a new thing today.
  • 0
    @HollowKitty Never seen that before. Kinda clever but also kinda hacky. Would be far cleaner to just put the code in its own method.
  • 0
    That is very interesting.
  • 0
    @BooFar what tha hell r u talking 'bout?

    hope u r being sarcastic 'cause this abomination has no justification
  • 0
    @Fast-Nop Macros?

    Are you talking about things like Microsoft word macros?

    I'd really like to understand what are you talking about
  • 0
    @arcioneo #defines.

    This covers the general idea quite well:
    https://stackoverflow.com/questions...
  • 0
    There exists the slash
  • 0
    @arcioneo in C, you can define text stuff that will automatically be replaced by other text stuff everywhere it occurs. That can be used to look like a function with the advantage that you don't have the function call overhead. The disadvantage is code bloat, so you won't use that for big stuff where the call overhead would be neglectable anyway. There are some traps to be considered, especially with regard to side effects like the ++, so it's a bit dangerous.

    @Andrew221 several statements, i.e. separated by semicolon, within the same line will still run out of control.
  • 0
    @aether Who in hell would create such a mess like that?

    Is not a function a Macro? come on
  • 1
    @arcioneo ??? Your comment is a little confusing and I honestly can’t understand the intent of what you are saying.

    I think you either completely misunderstand c macros or what I was talking about. The use-case of the do-while(0) in the context of c function macros (i.e. #defines that take parameters) which are multi-statement is to ensure the macro is treated in all context as a single block statement.

    I am not debating over using multi-statement macros, personally I feel they should be and can be avoided in most cases but there may be some reasons why one might want to use them.

    Basically what I am getting at is what @Fast-Nop is referring to is 100% correct and isn’t really a matter of debate. If you have a multi statement macro in c then you should be doing this otherwise someone might try to use it in a way that can cause some really hard to debug issues.
Add Comment