When I say "goto", my parrot says "Spaghetti Code"
-
Quote:
Are we asking annoying questions, just for the sake of asking?
No, not just for the sake of asking, just for the sake of annoying. :) Also to see how many mind readers would jump on me for using them - even though I didn't say I used them. :) And don't, except in rare, low-level performance cases. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
RandyBuchholz wrote:
even though I didn't say I used them. :)
That's not relevant to me, you're implying they may not be bad.
RandyBuchholz wrote:
This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
No, it hasn't. We have simple rules for the monkeys to follow; anyone who can actually think doesn't need them. So we can't do without that faith, since some people continously ignore everything in the manual. While I agree that a single goto won't kill anyone, I also haven't seen any practical C# example where using one would be justified.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
This old chestnut again :sigh: there are better constructs available with less risk of abuse. I've not used a goto in well over 20 years and am quite happy to leave it that way.
Never underestimate the power of human stupidity RAH
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
'the imperative goto == the object oriented if', both have their uses and allow misuse. The degree of misuse is telling ...
-
GuyThiebaut wrote:
Back in 1988
GuyThiebaut wrote:
the first year had 2 hours of computer time each week. Most of our work was done on paper.
I remember those days well!...being a CS major from around '87 to '89 when my part-time afternoon job turned into full-time, which meant almost no lab time. I quit school for 10 years and finally went back and finished. During my 10 year hiatus, I had very little contact with computers so coming back to it after a decade I was more than a little pleased to find that I could finally do homework at home...even in the wee hours of the morning! :-D :-D Somewhere in a box, in a closet, are notebooks with handwritten C, Pascal, and Fortran homework assignments. I also have some greenbar printouts with notes/grade. Deliverables for an assignment were typically handwritten pseudo-code and/or flowchart and the printout which contained source code and results. That junk probably won't make the next move! :laugh: It amazes me now to think about how different it was to learn programming back then. Nowadays, an answer to a question is usually just a few clicks away, back then we relied on books, our own wits, and occasionally teachers/aides who sometimes knew little more than we did. :) Thanks for the memories! :)
"Go forth into the source" - Neal Morse
Thanks for the post! I am ambivalent as to whether I would rather learn CS now or back in the 80's. Back in the 80's there was no WWW or everything that came with it(given the internet did exist with JANET being the network we had access to). In some ways I think people have it harder nowadays because there is so much more to learn. On the other hand a kid with a laptop and network connection can easily educate themselves, to the same level I was at when I graduated, before they even start their course in CS. I think we expect more of graduates nowadays than we did back in the 90's when it was a given that your first few years would be on the job training and you understood that when you graduated that you basically knew very little.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
-
I would say your examples are good examples of misuse, and not problems with `goto` itself. I don't use `goto` myself, except in rare cases to short-circuit a function.
That's a nice spot to use goto, I have used it twice in my life precisely for that: later I ended up removing them due to a refactor that improved every aspect of those functions (performance, readability, debuggability). That said, I don't discard it out of hand as it is a powerful tool. As TNCaver said "You think goto is evil: try writing Assembly programs without JMP" (it has been in my signature for years... and I do also write Assembly code so it was really appropriate).
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
-
I would say your examples are good examples of misuse, and not problems with `goto` itself. I don't use `goto` myself, except in rare cases to short-circuit a function.
You can use goto to short-circuit time-constrained algorithms for academic or scientific purposes. Don't use it anywhere else, especially not in a professional setting, or people will be strongly compelled to gut, rape and murder your code the first chance they get. And can you really blame them for it?
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
As with any instruction in any language, improper use will get you into trouble. There are some 470,000 words in Webster's dictionary. Day-to-day we typically use less than 500. The others are there just in case but that doesn't mean we have to use them. The closer to bare metal you get, the more the higher level languages simply carry too much overhead to be practical. In resource-constrained environments you don't have the luxury of being elegant. On a 32K platform you're limited to about 3-5,000 lines of (C) code, you need to be creative and shortcuts save program space. When the need arises for an assembly stub, branches and jumps (assembly level GOTOs) are a way of life. The GOTO instruction is a tool and like any tool, use it if you need it, but don't use it just because it's there. As it is said, if the only tool in your box is a hammer, you're constrained to build everything with nails, but if you need to nail something, it's useful (not necessary) to have a hammer.
-
It creates functions which are strictly monolithic. It's not bad but it create impediments in further expansions or refactorizations of the function that uses it. Goto to code outside the function containing it is problematic for the compilers, breaks modularity in C code as much as global variables do, and is completely undoable in OOP due to context changes. All in all it may be a good solution but ultimately not worth the delayed troubles if not under very constrained circumstances.
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
den2k88 wrote:
breaks modularity in C code
That is true for C, but not so much for C++.
goto
is context dependent in C++, it will call appropriate constructors/destructors. When used in local context it will not make the function/method monolithic. So ifgoto
is so bad, you might be using a badly outdated language? -
den2k88 wrote:
breaks modularity in C code
That is true for C, but not so much for C++.
goto
is context dependent in C++, it will call appropriate constructors/destructors. When used in local context it will not make the function/method monolithic. So ifgoto
is so bad, you might be using a badly outdated language?Plamen Dragiyski wrote:
goto
is context dependent in C++, it will call appropriate constructors/destructors.Which is an Access Violation waiting to happen when the code changes.
Plamen Dragiyski wrote:
So if
goto
is so bad, you might be using a badly outdated language?And that would be?
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
This question has received lots of humorous responses, now for a slightly more serious one ;) Yes, we used to use gotos a lot in the old days (and I'm actually old enough to say that :)). But my feeling now is that any language where I ever feel the need to use a goto is a flawed language. But seriously, the original "Go To Considered Harmful" [https://homepages.cwi.nl/~storm/teaching/reader/Dijkstra68.pdf\] was a brilliant step in providing discipline to coding to narrow the gap between algorithm design and implementation. A main idea being that gotos break the provability of correctness. Anybody can tell you that extensive use of gotos was involved in large numbers of defects (thus quite costly). One other point I'd like to make is that gotos can interfere with optimizers.
-
Plamen Dragiyski wrote:
goto
is context dependent in C++, it will call appropriate constructors/destructors.Which is an Access Violation waiting to happen when the code changes.
Plamen Dragiyski wrote:
So if
goto
is so bad, you might be using a badly outdated language?And that would be?
GCS d-- s-/++ a- C++++ U+++ P- L+@ E-- W++ N+ o+ K- w+++ O? M-- V? PS+ PE- Y+ PGP t+ 5? X R+++ tv-- b+(+++) DI+++ D++ G e++ h--- ++>+++ y+++* Weapons extension: ma- k++ F+2 X
den2k88 wrote:
Which is an Access Violation waiting to happen when the code changes.
How? If jumping outside block can causes access violation, then every "}" is potential access violation. If you talk about inter-scope
goto
, that's is entirely different story. You can do that in C using signals. What will be the state in C++ after suchgoto
it is indeed unknown, so that's one possible misuse ofgoto
. But if all affected variables are on the stack (and in C++ they should be - all pointer should be wrapped into appropriate *_ptr object allocated on the stack), jumping outside of scope is completely safe. Jumping inside non-conditional scopes is also safe. Jumping inside anything else is a bad idea. Jumping outside scope, can be realized by do-while wrapping:do {
// ... you can use break here
} while(false);But it cannot be used inside loop/switch statement and it is utterly more ugly than
goto
. -
Quote:
Are we asking annoying questions, just for the sake of asking?
No, not just for the sake of asking, just for the sake of annoying. :) Also to see how many mind readers would jump on me for using them - even though I didn't say I used them. :) And don't, except in rare, low-level performance cases. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
Java replaced
goto
with labeledbreak
andcontinue
. They keptgoto
as a reserved keyword that you cannot use. I wish C# had followed that model, but I suspect MS decided it was safer to follow C++ (which followed C) for legal reasons. (lawyers designing coding languages!) C++ made some uses of goto illegal that were allowed in C due to constructor/destructor scope constraints. -
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
Edsger Dijkstra (March 1968). "Go To Statement Considered Harmful". Communications of the ACM (PDF). 11 (3): 147–148. doi:10.1145/362929.362947. The unbridled use of the go to statement has as an immediate consequence that it becomes terribly hard to find a meaningful set of coordinates in which to describe the process progress. ... The go to statement as it stands is just too primitive, it is too much an invitation to make a mess of one's program.
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
The problem with goto is it makes it trivially easy to write spaghetti code. It is also the one of few jump methods available to a computer, so once you get down to the hardware goto (jmp) can be found nearly everywhere. On the other hand, you can write spaghetti code in any language, even those without goto, simply by not factoring any control structures properly.
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
goto overuse makes it very difficult to understand and debug a program. There are legitmate uses of goto, such as breaking out of a loop, but in those cases I would just use the keywords that do the same thing for clarity. I had the joy to try and debug someone's code that was riddled with gotos. He also had the nasty habit of making functions several hundred lines long. I ended up just rewriting everything he wrote. I still feel that was the correct decision, and it only took me a couple days.
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
Every programmer is taught that goto leads to spaghetti code. When they are new, they struggle to do without goto, adding termination variables and writing weirdly nested loops. They pass through an intermediate stage where they occasionally give in to the urge to use goto, typically to get out of a deeply nested loop or in case of error. They then author threads like this one, seeking to justify their need for goto to the world as, "It's not so bad if used ." If they don't change careers, and if they keep on improving their skills, developers eventually reach a point where the last time they used a goto is receding in the rearview mirror of time. They write smaller, simpler functions. They don't nest loops so much because they get better at understanding termination conditions. They learn to do tricks with break and continue, which provides somewhat more structured alternatives to the goto. And they read threads like this one and sigh.
-
I've not used the
goto
keyword in any of my C/C++/C# code over the last 20 years or so. I routinely usebreak
(goto
's more civilized brother, as it were) to exitfor
loops early. I do this in cases where expressing the iteration as afor
loop is more appropriate for the general case, and thebreak
handles the exceptions. I don't tend to usecontinue
nearly as much in similar situations; I'm not sure why. I dislike when code uses exceptions as an exotic form of iteration/flow control. My least favorite mechanism would have to besetjmp()/longjmp()
, which is a very old C runtime library mechanism that fortunately doesn't see a lot of use today.Software Zen:
delete this;
Yup, been there. Just after starting my first "real" job in the early PC days (company upgraded the network file server to a bleeding-edge IBM AT just after I started), I had to debug a DOS BASIC program that implemented the equivalent of function/subroutine calls by saving the return line in a variable (all global, single letter optionally followed by a single digit), setting an ON ERROR GOTO for the desired function, then forcing an exception, and using the same mechanism with the saved return line value to return. Written by a nuclear physicist, no REMarks in the code - just the physicist's hand-scrawled notes. I might be mistaken, but I seem to recall that there was a way to set the ON ERROR GOTO for different target lines depending on the exception code generated, and this was used to pick which "function" to "call" at various locations in the code. Today, he probably would have coded the entire thing as a single Excel macro with just Row-Column cell references and of course, no labels, headings, etc. :laugh:
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.
I rarely use "goto", but when I do it is usually: - to "break" twice out of some construct. (goto just_past_outer_loop) - (C) to implement an error handling construct with a clean exit, that jumps to a dedicated piece of rollback code, like
result = criticalaction1();
if (!result) goto error_exit;
result = criticalaction2();
if (!result) goto rollback_action1;
return SUCCESS;rollback_action1:
undoaction1()
error_exit:
return FAILURE;"spaghetti code" is a lazy, pseudocritical kind of comment. I hate it with a passion. If a Pavlov response is the only thing you're capable of, you should pick another job. I'm serious.
-
Mention "goto" to many programmers and they'll say, "Never use them, they lead to spaghetti code." It's a conditioned response. It seems to be a definition - "Dad, what's for dinner?" - "We're having `goto`." - "Again?". Ask them to explain why it is so bad, and you'll likely get a blank stare, or they just chant "spaghetti, spaghetti, …" Of course, a misused `goto` can lead to spaghetti code, but a (misused) [`any reserved word`] can lead to [`some bad thing`]. Have we developed an irrational fear of `goto` born out of ancient coding dogma? Or is `goto` inherently and absolutely evil? :) Update: As suggested by englebart, I'm adding why I asked this question. It came out of a recent discussion. I was reviewing some code someone showed me (they didn't write it) that had a `goto` in it. He said the code was Spaghetti Code. When I asked why, he said because it had a `goto`. I asked why that made it spaghetti, and all he could come up with was that he was taught that. I asked about a few other "programming truths", and had much the same response. This is good, that is bad, but I don't really know why. I started thinking about how for some things, aspects of programming have become more faith than science.