3

Is goto all that bad?

 2 years ago
source link: https://dev.to/pandademic/is-goto-all-that-bad-4103
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

What's wrong with goto?

Discussion (9)

pic

Collapse

Expand

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.

Comment button Reply

Collapse

Expand

Author

Jan 15

I get it know , thanks

Comment button Reply

Collapse

Expand

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.

Comment button Reply

Collapse

Expand

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

Comment button Reply

Collapse

Expand

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.

Comment button Reply

Collapse

Expand

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.

Comment button Reply

Collapse

Expand

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 🤔

Comment button Reply

Collapse

Expand

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.

Comment button Reply

Collapse

Expand


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK