Skip to main content

Timeline for Is using goto ever worthwhile?

Current License: CC BY-SA 4.0

9 events
when toggle format what by license comment
Jul 19, 2022 at 14:21 comment added Sidharth Ghoshal I guess in that last edge case a goto might just be justified
Jul 19, 2022 at 14:21 comment added Sidharth Ghoshal so adding a break statement into the loop definitely makes it worse, adding an if check into the loop also makes it worse. Some languages like python support a null return for void methods. So the code could say if (!ptr) { print("Exiting"); return; and that seems even more direct than jumping with the GOTO. Now if theres a bunch of context required to exit, you could write a helper method void Returner(arg1 ag2 ...) and then your code is if (!ptr) { return Returner(ptr, ...) }. If you have a TON of context this still sucks, since you might have a 10+ argument method call.
Jul 7, 2021 at 10:55 comment added klutt @AndrewHenle In this case, it's because I want to run the print statement upon exiting.
Jul 7, 2021 at 10:50 comment added Andrew Henle And it also involves the philosophy of single return That's the real problem: force-fitting a square algorithm that is simply implemented with multiple return statements into the round hole of "every function may have only one return". "Must cram all algorithms into this one pattern"? Even if they don't fit? You have to make the code more complex just to force-fit it into a rule that's supposedly for making code easier to understand and more maintainable?
Jul 7, 2021 at 10:04 history edited klutt CC BY-SA 4.0
added 18 characters in body
Jul 7, 2021 at 9:23 comment added Steve What's still not clear is why you would use goto at all, when the simple cases (like yours) are so easily replaced by purely structured blocks? The diktat against gotos, after all, is because they violate the regularity of structured blocks. Neither of your blocks, once entered, actually exit in accordance with the block principle (that is, the execution of a given block is followed by the execution of the first line after the given block).
Jul 7, 2021 at 7:12 comment added klutt @Steve I do not condone weaves of goto. That's where the problem arises.
Jul 7, 2021 at 6:33 comment added Steve I don't see that the goto "gives more information" in these cases. The first jump can be incorporated into an if-block, and the second jump can be converted into a break (if not elided completely by moving the break condition into the while-block condition). And although the label makes the jump target unambiguous, structured blocks are similarly unambiguous. Anyway the risks with gotos do not emerge in such simple examples as these, but only when they form a weave (and thus the order of operations, and the number of paths, becomes increasingly difficult to see).
Jul 6, 2021 at 21:28 history answered klutt CC BY-SA 4.0