What's wrong with goto?
For further actions, you may consider blocking this person and/or reporting abuse
What's wrong with goto?
For further actions, you may consider blocking this person and/or reporting abuse
Kure Dickson -
Bruun Damm -
professaywriters.com -
begreenman.com -
Top comments (11)
Consider a very primitive programming environment where there are only global variables, no functions/procedures, and no
control structures like if statements orloops. In order to write a program in such a system, you'd have to jump around in the code from one label to another using goto, modifying global variables as you go along. This becomes extremely hard to understand and maintain as a program gets bigger.In general it's definitely helpful to have structured code with loops, conditionals, and named functions that have their own variable scope.
You're mostly describing assembly, or BASIC. BTW, you can't write programs without
if
-like statements. Even assembly has them.Thanks, you're right. That was definitely a brain lapse on my part! Indeed, I was thinking of old basic or perhaps some of the older machine languages as well.
You weren't weened on BASIC I can tell. The response to GOTO historically, is strongly related to its ubiquity in some early contexts and the resulting phenomenon of unmanageable spaghetti code (thus named because it conjured the image of lots of intertangled code paths [noodles] with no visible rhyme or reason).
Of course prudent use of a single goto or two here or there remains in some contexts, not least the tenacious and still encountered BATCH programming context on Windows. And when a simple script contains a few gotos well structured to capture missing IF/THEN features with a focus on maintainability and clear code, they're livable, not some kind of existential crisis.
I get it know , thanks
The others mention the logical control problems which were first addressed by Dijkstra in 1968 but there is a more sinister problem with goto. Modern CPU's heavily relies on caching for performance improvement. When you use goto you may simply invalidate caches so CPU has to read all data from lower layers of memory which may cause CPU stall. And also you might cause a stack unwinding error which may corrupt memory. But in Linux kernel there are goto's so what is the logic here ? Goto is one is seemingly simple but a very advanced command which is very hard to master. You not only must know how your code is executed on the CPU but where your code and data is in memory system and what is the status of the stack while your goto is executed. Thats why although it's not recommended to use goto no one is removing them from the language. It's a powerful tool which must be used wisely.
Half off-topic, but:
I have been a developer for over 15 years now. Using C, Java, PHP, JS, Rust, etc.
Somehow I never ever used goto once in my life π€
Goto results in more complex code where it becomes more difficult to determine the potential execution paths of your code. Consider the following pseudo code
And compare it to the following.
Semantically they do the same thing, but one is more declarative in style. Then imagine having multiple layers of nested goto statements, etc - The thing pretty rapidly turns in "spaghetti" ...
Used judiciously, no.
I use it myself. If you want an example of a large popular project, then look no further than the Linux kernel. Here's an example of how it's generally used there.
Thanks !
Good information!! casting revenge spells