Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. When I say "goto", my parrot says "Spaghetti Code"

When I say "goto", my parrot says "Spaghetti Code"

Scheduled Pinned Locked Moved The Lounge
questiondiscussionannouncementlearning
45 Posts 29 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R RandyBuchholz

    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.

    B Offline
    B Offline
    bleahy48
    wrote on last edited by
    #36

    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.

    1 Reply Last reply
    0
    • R RandyBuchholz

      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.

      O Offline
      O Offline
      obermd
      wrote on last edited by
      #37

      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.

      1 Reply Last reply
      0
      • R RandyBuchholz

        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.

        U Offline
        U Offline
        User 13612524
        wrote on last edited by
        #38

        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.

        1 Reply Last reply
        0
        • R RandyBuchholz

          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.

          S Offline
          S Offline
          SeattleC
          wrote on last edited by
          #39

          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.

          1 Reply Last reply
          0
          • G Gary Wheeler

            I've not used the goto keyword in any of my C/C++/C# code over the last 20 years or so. I routinely use break (goto's more civilized brother, as it were) to exit for loops early. I do this in cases where expressing the iteration as a for loop is more appropriate for the general case, and the break handles the exceptions. I don't tend to use continue 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 be setjmp()/longjmp(), which is a very old C runtime library mechanism that fortunately doesn't see a lot of use today.

            Software Zen: delete this;

            N Offline
            N Offline
            Navanax
            wrote on last edited by
            #40

            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:

            1 Reply Last reply
            0
            • R RandyBuchholz

              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.

              M Offline
              M Offline
              Martijn Smitshoek
              wrote on last edited by
              #41

              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.

              1 Reply Last reply
              0
              • R RandyBuchholz

                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.

                T Offline
                T Offline
                TNCaver
                wrote on last edited by
                #42

                My sig line says it all.

                If you think 'goto' is evil, try writing an Assembly program without JMP.

                1 Reply Last reply
                0
                • R RandyBuchholz

                  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.

                  A Offline
                  A Offline
                  AnotherKen
                  wrote on last edited by
                  #43

                  I used basic in the past and for that language GOTO was very convenient for establishing looping and simple procedural content. The reason many teachers discourage it's use is that it can very easily lead to the development of spaghetti code. That said, the presence of a GOTO statement should not be seen as an automatic sign of spaghetti code. GOTO can be used to produce good code that is in no way likely to be judged negatively by a true computer scientist (at least on that basis, alone).

                  1 Reply Last reply
                  0
                  • R RandyBuchholz

                    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.

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #44

                    RandyBuchholz wrote:

                    Have we developed an irrational fear of goto born out of ancient coding dogma? Or is goto inherently and absolutely evil?

                    Ask anyone any of the following - Which is better Windows or Linux - Which is better C#, Python, Java or C++ - Which is better Relational databases or NoSQL databases - Which is better micro-services or traditional server based solutions. - Which is better Waterfall or Agile. - Ask them if they write maintainable and readable code. And of course then follow up with asking them to specify exactly what objective measurements they took to validate their response. Or even ask them how they would objectively measure it if they had the time and money.

                    1 Reply Last reply
                    0
                    • D den2k88

                      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

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #45

                      den2k88 wrote:

                      Which is an Access Violation waiting to happen when the code changes.

                      And you are claiming that cannot happen without gotos? Or if those others occur they can only occur when another feature of the language is used correctly?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups