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.

    D Offline
    D Offline
    den2k88
    wrote on last edited by
    #2

    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

    R P 2 Replies 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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #3

      Looking to some MSDN examples, I never saw goto, I only saw things like this (also a kind of goto): .... while(true) { if (!Method_1()) break; ... if (!Method_N()) break; ... } ...

      It does not solve my Problem, but it answers my question

      R 1 Reply Last reply
      0
      • L Lost User

        Looking to some MSDN examples, I never saw goto, I only saw things like this (also a kind of goto): .... while(true) { if (!Method_1()) break; ... if (!Method_N()) break; ... } ...

        It does not solve my Problem, but it answers my question

        R Offline
        R Offline
        RandyBuchholz
        wrote on last edited by
        #4

        There are `goto`s in the source code for C# in places.

        1 Reply Last reply
        0
        • D den2k88

          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

          R Offline
          R Offline
          RandyBuchholz
          wrote on last edited by
          #5

          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.

          D K 2 Replies 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.

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #6

            Hi,

            RandyBuchholz wrote:

            It's a conditioned response.

            That's exactly what it is. A few years ago I had to use some goto statements in a device driver written in C where the push/pop function prolog/epilog[^] was causing a performance reduction of between 30% - 40%. I could not find anything that matched the speed of the goto statement and left it in the production code. High-performance code is almost always ugly.

            RandyBuchholz wrote:

            Or is goto inherently and absolutely evil?

            I would say that you do not need a goto statement 99.999% of the time. But I have used it at least ONE time in the past two decades where I feel it was necessary. Best Wishes, -David Delaune

            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.

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #7

              I do not use goto in C++ programs while I use it in C ones. I do abuse of gotos in assembly code.

              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.

                C Offline
                C Offline
                CodeWraith
                wrote on last edited by
                #8

                RandyBuchholz wrote:

                Have we developed an irrational fear of

                Speak for yourself. :-) Just look what I'm working on right now. Every opcode that starts with B is a branch (= goto). :-)

                ; =========================================================================================
                ; BIOS Command selection
                ;
                ; Parameters:
                ; ---
                ;
                ; Returns:
                ; ---
                ; =========================================================================================

                BIOS_Command: LDI hi(TxtEnterCommand)
                STXD
                LDI lo(TxtEnterCommand)
                STXD
                SEP R4
                dw WriteLine

                CMD_Repeat: LDI 00FFH ; get RS232 input, max. timeout
                STXD
                SEP R4
                dw RS232InSoftware

                				IRX								; timeout?				
                				LDX
                				BZ   CMD\_Skip
                
                				STXD							; echo input
                				STXD
                				SEP	 R4
                				dw   RS232Out
                
                				IRX
                				LDX
                				SDI  006EH						; test input = n	
                				BZ   CMD\_Exit
                				
                				LDX
                				SDI  0079H				
                				BNZ  CMD\_Repeat
                
                				LDI  hi(TxtNewLine)				; announce BIOS CMD
                				STXD
                				LDI  lo(TxtNewLine)
                				STXD
                				SEP  R4
                				dw   WriteLine
                
                				LDI  hi(TxtNewLine)
                				STXD
                				LDI  lo(TxtNewLine)
                				STXD
                				SEP  R4
                				dw   WriteLine
                
                				LDI  hi(TxtCmdTitle)
                				STXD
                				LDI  lo(TxtCmdTitle)
                				STXD
                				SEP  R4
                				dw   WriteLine
                

                CMD_Prompt: LDI hi(TxtCmdPrompt) ; output the prompt
                STXD
                LDI lo(TxtCmdPrompt)
                STXD
                SEP R4
                dw WriteLine

                				LDI  00H						; get RS232 input, no timeout
                				STXD
                				SEP  R4
                				dw   RS232InSoftware
                				LDI  TRUE
                				PLO  RF
                
                				IRX								; echo input								
                				LDX
                				STXD
                				STXD
                				SEP	 R4
                				dw   RS232Out
                				
                				IRX				
                				LDX								; test input = x
                				SDI  0078H				
                				BZ   CMD\_Exit
                
                				LDX								; test input = X
                				SDI  0058H				
                				BZ   CMD\_Exit
                
                				LDX								; test input = h
                				SDI  0068H				
                				BZ   CMD\_Help
                
                				LDX								; test input = H
                				SDI  0048H				
                				BZ   CMD\_Help
                

                CMD_Invalid: LDI hi(TxtCmdInvalid) ; error message (invalid command)
                STXD
                LDI lo(TxtCmdInvalid)
                STXD
                SEP R4
                dw WriteLine
                BR CMD_Prompt

                CMD_Help: LDI hi(TxtCmdHelp) ; help text
                STXD
                LDI lo(TxtCmdHelp)
                STXD
                SEP R4
                dw WriteLine

                				LDI  hi(TxtCmdHelp1)
                				STXD
                				LDI  lo(TxtCmdHelp1)
                				STXD
                				SEP  R4
                				dw   WriteLine
                				BR
                
                R 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
                  Marc Clifton
                  wrote on last edited by
                  #9

                  Sacrilege! You will goto hell for such thoughts! Oh wait. Even hell has been refactored: while (true) burnInHellForever(); ;)

                  Latest Article - Building a Prototype Web-Based Diagramming Tool with SVG and Javascript Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                  R 1 Reply Last reply
                  0
                  • C CodeWraith

                    RandyBuchholz wrote:

                    Have we developed an irrational fear of

                    Speak for yourself. :-) Just look what I'm working on right now. Every opcode that starts with B is a branch (= goto). :-)

                    ; =========================================================================================
                    ; BIOS Command selection
                    ;
                    ; Parameters:
                    ; ---
                    ;
                    ; Returns:
                    ; ---
                    ; =========================================================================================

                    BIOS_Command: LDI hi(TxtEnterCommand)
                    STXD
                    LDI lo(TxtEnterCommand)
                    STXD
                    SEP R4
                    dw WriteLine

                    CMD_Repeat: LDI 00FFH ; get RS232 input, max. timeout
                    STXD
                    SEP R4
                    dw RS232InSoftware

                    				IRX								; timeout?				
                    				LDX
                    				BZ   CMD\_Skip
                    
                    				STXD							; echo input
                    				STXD
                    				SEP	 R4
                    				dw   RS232Out
                    
                    				IRX
                    				LDX
                    				SDI  006EH						; test input = n	
                    				BZ   CMD\_Exit
                    				
                    				LDX
                    				SDI  0079H				
                    				BNZ  CMD\_Repeat
                    
                    				LDI  hi(TxtNewLine)				; announce BIOS CMD
                    				STXD
                    				LDI  lo(TxtNewLine)
                    				STXD
                    				SEP  R4
                    				dw   WriteLine
                    
                    				LDI  hi(TxtNewLine)
                    				STXD
                    				LDI  lo(TxtNewLine)
                    				STXD
                    				SEP  R4
                    				dw   WriteLine
                    
                    				LDI  hi(TxtCmdTitle)
                    				STXD
                    				LDI  lo(TxtCmdTitle)
                    				STXD
                    				SEP  R4
                    				dw   WriteLine
                    

                    CMD_Prompt: LDI hi(TxtCmdPrompt) ; output the prompt
                    STXD
                    LDI lo(TxtCmdPrompt)
                    STXD
                    SEP R4
                    dw WriteLine

                    				LDI  00H						; get RS232 input, no timeout
                    				STXD
                    				SEP  R4
                    				dw   RS232InSoftware
                    				LDI  TRUE
                    				PLO  RF
                    
                    				IRX								; echo input								
                    				LDX
                    				STXD
                    				STXD
                    				SEP	 R4
                    				dw   RS232Out
                    				
                    				IRX				
                    				LDX								; test input = x
                    				SDI  0078H				
                    				BZ   CMD\_Exit
                    
                    				LDX								; test input = X
                    				SDI  0058H				
                    				BZ   CMD\_Exit
                    
                    				LDX								; test input = h
                    				SDI  0068H				
                    				BZ   CMD\_Help
                    
                    				LDX								; test input = H
                    				SDI  0048H				
                    				BZ   CMD\_Help
                    

                    CMD_Invalid: LDI hi(TxtCmdInvalid) ; error message (invalid command)
                    STXD
                    LDI lo(TxtCmdInvalid)
                    STXD
                    SEP R4
                    dw WriteLine
                    BR CMD_Prompt

                    CMD_Help: LDI hi(TxtCmdHelp) ; help text
                    STXD
                    LDI lo(TxtCmdHelp)
                    STXD
                    SEP R4
                    dw WriteLine

                    				LDI  hi(TxtCmdHelp1)
                    				STXD
                    				LDI  lo(TxtCmdHelp1)
                    				STXD
                    				SEP  R4
                    				dw   WriteLine
                    				BR
                    
                    R Offline
                    R Offline
                    RandyBuchholz
                    wrote on last edited by
                    #10

                    I have no fears! :-D My education is EE, and I still see registers (68xx architecture) in my head no matter what language I'm programming in. I don't actually see the code that way, it's just kind of a background image. It's amazing how many developers don't know that the only branching a CPU has is a `goto`.

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

                      G Offline
                      G Offline
                      Gary Wheeler
                      wrote on last edited by
                      #11

                      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 1 Reply Last reply
                      0
                      • M Marc Clifton

                        Sacrilege! You will goto hell for such thoughts! Oh wait. Even hell has been refactored: while (true) burnInHellForever(); ;)

                        Latest Article - Building a Prototype Web-Based Diagramming Tool with SVG and Javascript Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny Artificial intelligence is the only remedy for natural stupidity. - CDP1802

                        R Offline
                        R Offline
                        RandyBuchholz
                        wrote on last edited by
                        #12

                        Not:

                        Quote:

                        while (true) burnInHellForever();

                        `while (false) burnInHellForever();` "true" believers go to heaven! ;P

                        1 Reply Last reply
                        0
                        • R RandyBuchholz

                          I have no fears! :-D My education is EE, and I still see registers (68xx architecture) in my head no matter what language I'm programming in. I don't actually see the code that way, it's just kind of a background image. It's amazing how many developers don't know that the only branching a CPU has is a `goto`.

                          C Offline
                          C Offline
                          CodeWraith
                          wrote on last edited by
                          #13

                          Even one as unconventional as this one here. :-)

                          I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                          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.

                            R Offline
                            R Offline
                            Rick York
                            wrote on last edited by
                            #14

                            I think it depends on the context. In C it's not so bad and I can think of a few notable examples where it is used. One instance I found rather humorous and posted in a coding 'hall of fame' page we used to have here, is in the default procedure of Windows v3.0 and the label used was "IcantBelieveIactuallyUsedAgoto." The source code of it was published in an old book by Peter Norton on windows programming. On the other hand, I think using goto in C++ is a really bad idea. Since objects are automatically deleted when they fall out of scope, a goto could result in memory leaks if not used very carefully. As far as I am concerned this opinion is not based on fear nor is it irrational. YMMV.

                            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.

                              G Offline
                              G Offline
                              GuyThiebaut
                              wrote on last edited by
                              #15

                              Back in 1988 when I went to university to study computer science we were taught that we were never to use goto except in one particular circumstance with COBOL. We were strictly taught structured programming and in the first year had 2 hours of computer time each week. Most of our work was done on paper. Fast forward to now and I am quite happy to use a return statement in code which in essence is a goto. I think in principle it's good to learn the rules of structured programming to know when it is ok to break those rules.

                              “That which can be asserted without evidence, can be dismissed without evidence.”

                              ― Christopher Hitchens

                              C K 2 Replies 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.

                                N Offline
                                N Offline
                                Nathan Minier
                                wrote on last edited by
                                #16

                                For several years programmers have had OOAD and OOP pounded into their heads, and goto is an inherently structured code construct. If you use goto in an object oriented program, you _are_ creating spaghetti code, which is why that particular reflex exists.

                                "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

                                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
                                  Maximilien
                                  wrote on last edited by
                                  #17

                                  I don't have an existentialism issue with goto. It can have its uses, very limited, but can be useful. We have a few in some of our older legacy C code. None in the new C++ code.

                                  I'd rather be phishing!

                                  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.

                                    OriginalGriffO Offline
                                    OriginalGriffO Offline
                                    OriginalGriff
                                    wrote on last edited by
                                    #18

                                    It's not an irrational fear: it's a sensible precaution. goto is a "shortcut" for lazy lecturers and lazy students - it positively encourages poor quality code and bad habits. That doesn't mean it shouldn't be used, just that you have to understand why you aren't using it before you should be allowed to use it. It has its place (try doing assembly code, or time critical embedded C / C++ without it) but generally speaking it's a bad idea in "modern" languages which have rich flow control constructs and a set of good practices which should mean that it isn't that necessary. For example, I've been coding in C# for nine years now, and not once have I needed to use a goto, nor have I had to "fudge round" not using one. It annoys me when it's taught early because the idiot running the course can't be bothered to teach a while, for, or foreach loop to beginners. Because once you start using a hammer, every problem looks like a nail...

                                    Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                                    "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
                                    "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

                                    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.

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #19

                                      RandyBuchholz wrote:

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

                                      Are we asking annoying questions, just for the sake of asking? Jumping to a label was once an improvement ovr jumping to a specific linenumber. You are doing as if using a "GOTO 40" still has a place in modern C# coding. It doesn't. No, it's not a dogma; you don't have to believe me and can feel free to GOTO anywhere in your code - it will be your problem, not mine :)

                                      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.

                                      R 1 Reply Last reply
                                      0
                                      • L Lost User

                                        RandyBuchholz wrote:

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

                                        Are we asking annoying questions, just for the sake of asking? Jumping to a label was once an improvement ovr jumping to a specific linenumber. You are doing as if using a "GOTO 40" still has a place in modern C# coding. It doesn't. No, it's not a dogma; you don't have to believe me and can feel free to GOTO anywhere in your code - it will be your problem, not mine :)

                                        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.

                                        R Offline
                                        R Offline
                                        RandyBuchholz
                                        wrote on last edited by
                                        #20

                                        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.

                                        L E 2 Replies Last reply
                                        0
                                        • G GuyThiebaut

                                          Back in 1988 when I went to university to study computer science we were taught that we were never to use goto except in one particular circumstance with COBOL. We were strictly taught structured programming and in the first year had 2 hours of computer time each week. Most of our work was done on paper. Fast forward to now and I am quite happy to use a return statement in code which in essence is a goto. I think in principle it's good to learn the rules of structured programming to know when it is ok to break those rules.

                                          “That which can be asserted without evidence, can be dismissed without evidence.”

                                          ― Christopher Hitchens

                                          C Offline
                                          C Offline
                                          CodeWraith
                                          wrote on last edited by
                                          #21

                                          GuyThiebaut wrote:

                                          use a return statement in code which in essence is a goto.

                                          No, it's that other thing, calling and returning from subroutines. A totally different animal, down to the machine code of the processor.

                                          I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                                          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