r/C_Programming • u/just_pascal_dev • Oct 17 '26
Goto is the worst command in C.
Goto is an old and hacky command in programming.
Goto was created in the Palaeozoic era. It was used for switching to line n.
Goto raises questions: “My editor doesn’t show line numbers, how can I use Goto?”
This command uncomfortable to use. Imagine, you have a book. You’re reading it, reading… And when you’re on page 5, it says: “Go to page 7, 10, 157, 21457, 8564588, 4…” Why?
You can replace Goto with:
- break/continue.
- return.
- Functions, in the end!
So, Goto is the worst command in all programming languages, including C.
MY OPINION MAY NOT COINCIDE WITH YOURS!!!
0
Upvotes
19
u/Classic_Department42 Oct 17 '26
Error handling is best done with goto, read some linux kernel source
23
u/clickyclicky456 Oct 17 '26
Beginners: use goto all the time because they don't know any better, get into all sorts of tangles with it
Intermediate users: learn from their noob mistakes, "goto is a terrible thing, never ever use it ever under any circumstances"
Experts: understand that in the right circumstances (which are rare) it can be the right tool for the job, use it in those cases and no others
Sounds like you're still in the intermediate stage of your journey.
10
u/shinmai_rookie Oct 17 '26
Goto doesn't raise that question because in C you don't use line numbers, you go to labels which you define in the code and are shown alongside it.
And at any rate this discourse feels like it belongs in the 80s, that goto is to be used scarcely if at all is now common sense.
7
u/Future_Pace_5290 Oct 17 '26
Exception handling needs it
34
u/dmc_2930 Oct 17 '26
Why do you think goto requires line numbers? That’s not a thing in C.
41
u/Jonatan83 Oct 17 '26
If you can't find a use for goto you haven't written code with good error handling and cleanup
Ah so you haven't actually used goto, gotcha.
12
u/aldunax Oct 17 '26
The entire Linux kernel development community politely disagrees with you.
Also, worth a read. There are some very good, very legitimate use cases for goto.
7
u/komata_kya Oct 17 '26
Reminds me of my professor who said the same thing. He also used 1 based arrays in c++ by simply ignoring the 0th element...
6
u/Total-Box-5169 Oct 17 '26
4
u/tstanisl Oct 17 '26
The "goto" is a tool. Like every other useful tool it has some sharp edges, maybe a bit larger than other tools of its kind. But those sharp edges are exactly what makes tools useful even though a bit dangerous.
Generally,
gotoworks nice for error handling in C. It's more concise, more readable and I would say even more elegant than other methods. Probably, advent ofdeferwill change it but we need to wait for C2Y to assess.Aditionally, some state machines can be naturally expressed in
goto.4
u/AliceCode Oct 17 '26
Goto is there for parity with assembly, which has jumps. It's just fundamentally how computers work. It's only a footgun if you don't know how to use it properly. If you don't like goto, then use a language that doesn't have it.
4
u/sci_ssor_ss Oct 17 '26
Skill issue, clearly
I mean:
Whaaaat ?
Read Rayuela, from Cortazar. It may help.
4
u/wwabbbitt Oct 17 '26
https://xkcd.com/292/
4
u/tux2603 Oct 17 '26
Goto in c uses labels, not line numbers. As long as you know what you're doing and understand how code actually runs there's no problems with it
Are you studying computer science?
3
u/gizahnl Oct 17 '26
Untill defer is common enough goto will be my goto cleanup routine, especially when having to deal with errors.
3
u/rainboww_J Oct 17 '26
It really helps if a function needs a bit of a cleanup before a return. Without a goto you might end up doing a lot of things at multiple places (i.e. when you've done some setup stuff like opening files or what not and want to gracefully close them all when something went wrong. You can close those handles at every error in-between but a simple goto cleanup is much cleaner in my opinion).
As long as one isn't using goto in strange ways or let it make jumps all over the place it's fine
edit: also... why are you needing line numbers for goto? C isnt BASIC 😅
2
u/limitless_grow Oct 21 '26
You don't have to use goto but goto is great to jump to the end of a function and do the local cleanup once the result is known e.g. in case of errors. Any "great" workaround for preventing a goto makes your code worse to read. Just as in real life - once you are done in a shop you are just going to the exit where you pay.
Why do you want to do this in your programs a lot more idiotic than in real life?
Beside of that the 'switch' statement in C and C++ is nothing but just a multi-goto so don't ever use that! 😄
The following code of a test function with argument checking and going to the function exit in case of invalid arguments is valid for C and C++ compilers and uses 'goto' as told before but also that evil 'switch' statement.
Which of both makes that code hard to understand?
2
u/dmc_2930 Oct 17 '26
I prefer the “comefrom” which is the opposite of goto.
(Also it’s not a real thing)
2
u/e-batters Oct 18 '26
I feel that error handling is a valid use case for goto:
if (FAILED(...)) goto handle_error_and_cleanup;It may not be useful apart from that, but for error handling
gotois just fine.1
u/Still_Explorer Oct 22 '26
I also when I first learnt programming, remember everyone would say that goto is bad. From personal experience I have found that there would be two times where it makes sense to use it, because it makes code very simple and straight-forward.
Though the same code can be rewritten without goto for sure, but kinda makes you consider the pros and cons. Such as adding extra code and more complicated logic (more execution paths) so there would be no goto statement at all.
When the break statement is ambiguous you do a goto. No problem to use a boolean variable instead outside `for` and break (last statement of the for loop) but you see the effort.
Also this one, you separate the algorithm from the execution paths. When you get an input you like/dislike you break the algorithm and stop the execution path. You wouldn't like to think about rewriting an algorithm from scratch to handle non-goto logic, though in a sense you can but why bother? You just goto and break from algorithm.
1
u/bird_feeder_bird Oct 26 '26
I dont see the issue, its basically just jmp. Like if you want multiple paths to lead to the same place its very convenient.
1
u/non-existing-person Oct 17 '26
Not really. It's more like, you are on page 5, and it says "goto line 1,12,14 ON CURRENT page". goto does not go beyond single function, and functions should be fairly short anyway
-1
u/just_pascal_dev Oct 17 '26
Ok, guys. I made a mistake, when I said, that Goto is useless. My bad.
0
u/zsaleeba Oct 17 '26
OP, have you read the incredibly famous paper by Dijkstra, "Go To Statement Considered Harmful"?
0
u/WittyStick Oct 17 '26
Serious response though - I basically never use
goto- there's virtually no need for it.goto *on the other hand, is useful, but non-standard.But now most uses of
goto *can be replaced withmusttail.