DEV Community

Pandademic
Pandademic

Posted on

Is goto all that bad?

What's wrong with goto?

Discussion (11)

Collapse
nestedsoftware profile image
Nested Software • Edited on

Consider a very primitive programming environment where there are only global variables, no functions/procedures, and no control structures like if statements or loops. 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.

Collapse
pauljlucas profile image
Paul J. Lucas

You're mostly describing assembly, or BASIC. BTW, you can't write programs without if-like statements. Even assembly has them.

Collapse
nestedsoftware profile image
Nested Software

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.

Collapse
thumbone profile image
Bernd Wechner • Edited on

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.

Collapse
pandademic profile image
Pandademic Author

I get it know , thanks

Collapse
bleakview profile image
Mustafa Unal

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.

Collapse
kayis profile image
K (he/him) • Edited on

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 🤔

Collapse
polterguy profile image
Thomas Hansen

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

var foo = 0;
while(foo++ < 20) { /* ... Do stuff ... */}
Enter fullscreen mode Exit fullscreen mode

And compare it to the following.

var foo = 0;
start:
/* ... do stuff ... */
if (++foo < 20) { goto start; }
Enter fullscreen mode Exit fullscreen mode

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" ...

Collapse
ac000 profile image
Andrew Clayton

Is goto all that bad?

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.

Collapse
pandademic profile image
Pandademic Author

Thanks !

Collapse
ellaallen profile image
EllaAllen

Good information!! casting revenge spells