r/AskProgramming • u/JarJarAwakens • Aug 07 '22
Other When is it appropriate to use GOTO?
I've heard it is a bad idea to use GOTO since it causes spaghetti code but there must be a valid reason it is present in many programming languages like C and C++. In what use cases is using GOTO superior to using other control structures?
12
Upvotes
17
u/YMK1234 Aug 07 '22
I recommend you read these two ...
The second gives some examples where goto actually makes the code more readable. YMMV how much that applies to the language of your choice though.
E: So to answer the question: in 99% of all cases probably not, but in some cases it actually does make sense.
12
u/muehsam Aug 07 '22
I'm only going to talk about C here.
Please read to the end, the last part is way more important than the examples I give for situations in which I might use
goto.Use it when it makes your program easier to read. Don't overuse it. It's a sharp tool.
A simple example where
gotocomes in handy is for when you want to break out of multiple loops. Another one would be centralized error handling: You have multiple different places in your function that all require the same few commands for error handling? Just put the error handling in the end of the function, after the successful return, and jump there when you encounter an error. I have also used it (very sparingly) for some loops now and then, when it's not really a "loop" in my mind, but more of a restart. I notice somewhere in my function that something doesn't work or certain parameters don't work well together? I set the parameters right and jump back to the start.The valid reason is that it's what the processor does, and what early programming languages did. All control flow was basically
if ... gotoor plaingoto. But we don't need to do that anymore, we have good control structures now.In general, forget the idea of "if it's a part of the programming language, that's probably for a good reason, and there must be some valid use cases". No, no, no. Programming languages have evolved over time, and once some feature has made it into a programming language, it's in there, even if that feature is bad, or if the problem it is meant to solve is eventually solved a different way. All programming languages have "bad parts" that you should avoid.
6
u/jddddddddddd Aug 07 '22
GOTOs are still somewhat common in C programs. The Linux kernel famously has tens of thousands of them, and similarly the Python interpreter has a tonne of them. If you’re working on a C code base where using GOTOs is the accepted style for error handling, you should follow suit. It’s also sometimes used for efficiency, since when you’re making an OS/Driver/compiler, etc. speed of execution is more important than code readability.
GOTOs are also pretty common in languages where for-loops aren’t labelled and therefor you can’t break out to the top level (you have to use a flag, and individually break from each level).
Finally, global-GOTOs are also common for error handling if something catastrophic has gone wrong, like the stack is fucked. You can’t ‘pop’ your way back up the stack, so you just jump to some function to do whatever cleanup you can, tell the user you’ve crashed, and then exit to the OS.
Finally, finally, I guess GOTOs are still used in languages where you don’t have anything else to work with. It’s been a long time since I’ve used it, but IIRC, SNOBOL has basically no program-flow functionality, so it’s just a bunch of jumping around. I guess the same could be said for most Assembly languages, since ‘jmp’ statements are all you have to work with.
4
u/suprjami Aug 07 '22
The concept of Single Entry, Single Exit in C relies on goto.
As others have said, the Linux kernel is full of these.
Done right, it does make code more legible and easier to follow.
However, make sure you're giving some hint about the failure before your goto jump. It sucks to find a function which exits with a dozen gotos but no clear reason which goto was used above.
1
u/[deleted] Aug 08 '22
If you can avoid it it's better in most cases.
In my 20+ years of programming there was only a single case when I would have used goto to make the code more legible, and we decided (with the PO who was also programming) to not use it because other people might missunderstand it.
It was in a function in where we needed to do some cleanup in every return. So it was something like this (simplyfied) in C++:
The improved version would be something like this:
The free was a bit more convoluted and the ifs were more nested, so it made more sense.
Also... we could have refactored it a bit to make it better, but I can't remember how it was exactly.
2
u/OracleGreyBeard Aug 07 '22
I would argue the flip side of that it's been specifically excluded from languages like JavaScript, Rust and Java. The "valid reason" for including a construct is that the language authors favored it. It's not necessarily any objectively intrinsic value.
That said, even in C# you would prefer a goto to break out of deeply nested loops. The other alternative "break" only breaks out of a single level.
2
u/onebit Aug 08 '22
You can have one goto per test case ;)
2
u/Gabe_b Aug 08 '22
making an infinite print loop in BASIC when you're 8.
2
u/Felicia_Svilling Aug 08 '22
No. That doesn't follow. Backwards compatibility is really important for programming languages. As such it is not uncommon that languages still includes features, even though the general recommendation is to never use those features.
Also note that C is a really old language, and that C++ was designed to be a super-set of C. People have not been designing language with goto operators in the last thirty years.
2
u/Blando-Cartesian Aug 08 '22
John Carmack’s balanced view seems to be that it’s useful, but rarely needed.
2
u/CartmansEvilTwin Aug 07 '22
Pretty much never.
It can be useful in generated code, when nobody's supposed to read it anyway, but that's pretty much it.
I literally never used goto. There's absolutely no need.
1
u/SarHavelock Aug 08 '22
Goto isn't harmful. People fear it because they do not understand. When used correctly, gotos are a powerful control statement, but are not for the weak-minded.