Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
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. -
BooFar456yI 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.
-
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.
-
v1shky846y@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
-
@v1shky C doesn't have try/catch. It does have setjmp/longjmp, but while/break is easier to read.
-
@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. ^^
-
aether626y@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.
-
@HollowKitty Never seen that before. Kinda clever but also kinda hacky. Would be far cleaner to just put the code in its own method.
-
arcioneo7706y@BooFar what tha hell r u talking 'bout?
hope u r being sarcastic 'cause this abomination has no justification -
arcioneo7706y@Fast-Nop Macros?
Are you talking about things like Microsoft word macros?
I'd really like to understand what are you talking about -
aether626y
-
@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. -
arcioneo7706y
-
aether626y@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.
Related Rants
Today at work while reading legacy code:
do {
...
} while(false)
Wanna cry and laugh at the same time
rant
shit happens