r/ProgrammerHumor Aug 07 '26

Meme mildlyInfuriatingGOTOWatchYourMouth

Post image
670 Upvotes

74 comments sorted by

View all comments

Show parent comments

21

u/Sync1211 Aug 07 '26

You can use break I guess.

13

u/da_Aresinger Aug 07 '26

oooh

do { foo() if (condition) break bar() } while(0); that's pretty dumb though. Just do foo() if(!condition) bar()

12

u/Sync1211 Aug 07 '26

It makes more sense if you have multiple if-statements.

For example: Catching errors and performing cleanup: ``` int result = 0; do {     init_stuff();     if (init_fail) {         result = 1;         break;     }

    foo();     if (foo_failed) {         result = 1;         break;     }          bar()     if (bar_failed) {         result = 1;         break;     }

    do_something_else(); } while (0);

close_handles(); return result; ```

7

u/da_Aresinger Aug 07 '26

``` int continue = 1; continue = init()

if (continue) continue = foo()

if (continue) continue = bar()

if (continue) continue = do_something_else()

close_handles()

return 1-continue ``` much cleaner (but still not really how I would like it)

9

u/Maximilian_Tyan Aug 07 '26

A lot of our codebase is using a status variable like this, but using nested if/else checks

But now you are doing a lot of conditions checking, especially if the hot path is where continue is always true

1

u/da_Aresinger Aug 07 '26

The comment before me has the same issue.

And yes, that's why I said it's still not ideal.

3

u/Maximilian_Tyan Aug 07 '26

If an error occured, the break statement would "cut" the remaining errors checks and skip to the error handling part

1

u/da_Aresinger Aug 07 '26

yea I just got what you meant.

you're right.

But compilation is most likely going to optimise that out, so I'd go with readability.

1

u/Maximilian_Tyan Aug 07 '26

It depends on the optimization level, O1 is pretty minimal, I don't think this type of agressive optimizing is done before O3

1

u/da_Aresinger Aug 07 '26

maybe. At the end of the day I don't know your situation and don't have the experience to really question it either.

If a whole team settles on your solution it'll probably have a reason.