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. Where Is IntelliSense For "goto" statements?

Where Is IntelliSense For "goto" statements?

Scheduled Pinned Locked Moved The Lounge
visual-studiocsharptutorialquestion
58 Posts 28 Posters 13 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.
  • L Leslie Sanford

    Richard Blythe wrote:

    I've been trying to dig up a good example of me using the "goto" statement.

    I haven't used goto's, but my understanding is that they can be helpful in error handling, e.g. goto HandleError. It allows you to write code after such goto statements in such a way that it can assume that no error has occurred, thus simplifying things. One could argue that this is what exceptions are for. But the same reasoning that says that goto's are bad would seem to apply to exceptions, maybe even more so. Using goto to jump ahead within a function to execute some error handling code would seem less confusing than throwing the control flow completely out of the current context to who knows where.

    T Offline
    T Offline
    Theraot
    wrote on last edited by
    #49

    That was a good point, and that's why we use pokémon error handling: to put a limit to that who knows where. The gotta catch'all approach is good when calling third party code in particular when it has the potential to be wrote letter by who knows who. By the way, I browsed my more relevant solution for an use of goto, and found only one, and I'm proud of it:

        \[DebuggerNonUserCode()\]
        private static int Sqrt(int number)
        {
            //Newton's method aproximation for positive integers
            //if (n == 0) return 0;
            int x, \_x = number >> 1;
        back:
            x = (\_x + number / \_x) >> 1;
            if (x <= \_x) return \_x;
            \_x = x;
            goto back;
        }
    

    I did my best at that time to increase performance of that code without going to a non-portable solution. I wonder if anybody will find a better way to write it.

    1 Reply Last reply
    0
    • D Dave Kreskowiak

      Richard Blythe wrote:

      I don't use the: "goto" statement very often but I have started using it more frequently.

      >SMACK!!< You what? :| I haven't found a need for a GOTO in the last, oh, 15 years...

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008
      But no longer in 2009...

      A Offline
      A Offline
      Antzzz
      wrote on last edited by
      #50

      Dave Kreskowiak wrote:

      >SMACK!!< You what? I haven't found a need for a GOTO in the last, oh, 15 years...

      You've obviously never coded in C for a low power micro with no exception handling... GOTO has it uses - mostly for bailing out to a common exit point when something goes pearshaped and you need to clean up after yourself. Only other option in these circumstances is horribly nested if statements - using a GOTO results in much cleaner/readable code. A TRY/CATCH block would do the trick nicely, but most low power (I'm not talking ARMs here, old crusty Renesas M16s and the like) micros used in industry don't have exception handling.

      D 1 Reply Last reply
      0
      • R Richard Blythe

        I don't use the: "goto" statement very often but I have started using it more frequently. Does anyone know why Visual Studio does not implement IntelliSense for goto statements? For example: // VS code editor does not show IntelliSense with //available "Label_" when entering this goto statement if (condition == true) goto Label_ExitCode; //... Label_ExitCode: //...

        The mind is like a parachute. It doesn’t work unless it’s open.

        S Offline
        S Offline
        Sergelp
        wrote on last edited by
        #51

        Ever heard of try/catch - if then else ... Goto usage is the ultimate spaghetti type programming, so you don't want to go that way. It's not a crusade, it's a fact. Believe me - a former spaghetti analyser that has programmed for years in language like Data General/UBB (Universal Business BASIC). For a long time in those languages (interpreter based) the "do while/for loop" instruction didn't exist and every line in your program had a linenumber. With the goto you could force a kind of do while or even if then else. Also a goto made the program go faster (interpreter) skipping the lines that it didn't needed. The goto was of course abused making the readability very poor. Here's an example of a do while in old BASIC 100 if i<10 then 110 ... (instruction) 120 ... 130 ... 140 goto 100 150 end if Gradually the linenumber became labels and vb3,vb4,... took on this way of programming. @start 100 if i<10 then 110 ... 120 goto @start 130 end if Those (nostalgic) days are gone now... :-) I think the only reason why MS didn't completely erase the command is backwards compatibility with VB6 where you had to use it with (on) error handling. Now you have an (even better) initiative for this, the try/catch/finaly block. The finaly part is certainly an improvement. Conclusion : "Spaghetti is nice as long as it's on your plate accompagnied with some tomatoesauce, cheese and some tabasco".

        modified on Tuesday, July 13, 2010 5:20 AM

        R 1 Reply Last reply
        0
        • R Richard Blythe

          I don't use the: "goto" statement very often but I have started using it more frequently. Does anyone know why Visual Studio does not implement IntelliSense for goto statements? For example: // VS code editor does not show IntelliSense with //available "Label_" when entering this goto statement if (condition == true) goto Label_ExitCode; //... Label_ExitCode: //...

          The mind is like a parachute. It doesn’t work unless it’s open.

          B Offline
          B Offline
          Bujar Tahiri
          wrote on last edited by
          #52

          Richard Blythe wrote:

          I don't use the: "goto" statement very often but I have started using it more frequently. Does anyone know why Visual Studio does not implement IntelliSense for goto statements?

          Try it in VB, maybe C# does not have it

          1 Reply Last reply
          0
          • S Sergelp

            Ever heard of try/catch - if then else ... Goto usage is the ultimate spaghetti type programming, so you don't want to go that way. It's not a crusade, it's a fact. Believe me - a former spaghetti analyser that has programmed for years in language like Data General/UBB (Universal Business BASIC). For a long time in those languages (interpreter based) the "do while/for loop" instruction didn't exist and every line in your program had a linenumber. With the goto you could force a kind of do while or even if then else. Also a goto made the program go faster (interpreter) skipping the lines that it didn't needed. The goto was of course abused making the readability very poor. Here's an example of a do while in old BASIC 100 if i<10 then 110 ... (instruction) 120 ... 130 ... 140 goto 100 150 end if Gradually the linenumber became labels and vb3,vb4,... took on this way of programming. @start 100 if i<10 then 110 ... 120 goto @start 130 end if Those (nostalgic) days are gone now... :-) I think the only reason why MS didn't completely erase the command is backwards compatibility with VB6 where you had to use it with (on) error handling. Now you have an (even better) initiative for this, the try/catch/finaly block. The finaly part is certainly an improvement. Conclusion : "Spaghetti is nice as long as it's on your plate accompagnied with some tomatoesauce, cheese and some tabasco".

            modified on Tuesday, July 13, 2010 5:20 AM

            R Offline
            R Offline
            Richard Blythe
            wrote on last edited by
            #53

            My primary reason is for complex validation that share the same exception. Here's an example:

            public void validateSomething(someComplexType)
            {
            if (first_conditionFails)
            goto Label_Error;
            else if (complexAlgorithmRequired)
            {
            //init vars for algorithm...
            if (first_conditionFails)
            goto Label_Error;
            else if (second_conditionFails)
            goto Label_Error;
            else //perform second phase of algorithm
            {
            //init vars for second phase...
            if (first_conditionFails)
            goto Label_Error;
            else if (second_conditionFails)
            goto Label_Error;
            }
            }
            //skip the end-of-routine error code
            return;

            Label_Error:
            //set class vars...
            //to an invalid state...
            //...
            throw new someException();
            }

            How would you implement this with try/catch without a whole bunch of "throw someException()"?

            The mind is like a parachute. It doesn’t work unless it’s open.

            E S 2 Replies Last reply
            0
            • R Richard Blythe

              My primary reason is for complex validation that share the same exception. Here's an example:

              public void validateSomething(someComplexType)
              {
              if (first_conditionFails)
              goto Label_Error;
              else if (complexAlgorithmRequired)
              {
              //init vars for algorithm...
              if (first_conditionFails)
              goto Label_Error;
              else if (second_conditionFails)
              goto Label_Error;
              else //perform second phase of algorithm
              {
              //init vars for second phase...
              if (first_conditionFails)
              goto Label_Error;
              else if (second_conditionFails)
              goto Label_Error;
              }
              }
              //skip the end-of-routine error code
              return;

              Label_Error:
              //set class vars...
              //to an invalid state...
              //...
              throw new someException();
              }

              How would you implement this with try/catch without a whole bunch of "throw someException()"?

              The mind is like a parachute. It doesn’t work unless it’s open.

              E Offline
              E Offline
              engchin
              wrote on last edited by
              #54

              You can put the codes under Label_Error in a separate function, say CatchError(). Then, replace every line of your goto label_error statement with a statement calling the CatchError function. This would be much more readable and at the same time avoid all the pitfalls of goto.

              1 Reply Last reply
              0
              • R Richard Blythe

                My primary reason is for complex validation that share the same exception. Here's an example:

                public void validateSomething(someComplexType)
                {
                if (first_conditionFails)
                goto Label_Error;
                else if (complexAlgorithmRequired)
                {
                //init vars for algorithm...
                if (first_conditionFails)
                goto Label_Error;
                else if (second_conditionFails)
                goto Label_Error;
                else //perform second phase of algorithm
                {
                //init vars for second phase...
                if (first_conditionFails)
                goto Label_Error;
                else if (second_conditionFails)
                goto Label_Error;
                }
                }
                //skip the end-of-routine error code
                return;

                Label_Error:
                //set class vars...
                //to an invalid state...
                //...
                throw new someException();
                }

                How would you implement this with try/catch without a whole bunch of "throw someException()"?

                The mind is like a parachute. It doesn’t work unless it’s open.

                S Offline
                S Offline
                Sergelp
                wrote on last edited by
                #55

                Hi Richard, I didn't know for for what purpose you were trying to use the goto that's why I assumed it was for as try catch system (is the most common use). On your case it's not the try/catch that has to be used. How I would solve this problem, is by using validationfunction with a return value. This way you could even put your validation in a separate class or library. It's a standalone function. Best practice to seperate this because if your using winforms, you could easily use the same validation when you decide to use ASP.net or something else. Compile the lib as a dll, put the function public and you can use it in any language. The enum is practical in .net because the intellisense kicks in but you could easily test it on the values 1,2,3 In fact you could consider the return value as a kind of a label. I'll put it in VB because I know this best but you'll get the idea. Enum ValidateError firstcondition = 1 secondcondition = 2 thirdcondition = 3 End Enum Private Sub ValidateMessage(ByVal someComplexType) Select Case ValidateSomething(someComplexType) Case ValidateError.firstcondition MsgBox("first") Case ValidateError.secondcondition MsgBox("second") Case ValidateError.thirdcondition MsgBox("third") Case Else MsgBox("ok") End Select End Sub Private Function ValidateSomething(ByVal someComplexType) As ValidateError If someComplexType < 0 Then Return ValidateError.firstcondition End If If someComplexType > 100 Then Return ValidateError.secondcondition End If If someComplexType > 1000 Then If someComplexType > 2000 Then Return ValidateError.thirdcondition End If End If '... End Function What do you think of it ? Regards Serge

                1 Reply Last reply
                0
                • A Antzzz

                  Dave Kreskowiak wrote:

                  >SMACK!!< You what? I haven't found a need for a GOTO in the last, oh, 15 years...

                  You've obviously never coded in C for a low power micro with no exception handling... GOTO has it uses - mostly for bailing out to a common exit point when something goes pearshaped and you need to clean up after yourself. Only other option in these circumstances is horribly nested if statements - using a GOTO results in much cleaner/readable code. A TRY/CATCH block would do the trick nicely, but most low power (I'm not talking ARMs here, old crusty Renesas M16s and the like) micros used in industry don't have exception handling.

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #56

                  Antzzz wrote:

                  You've obviously never coded in C for a low power micro with no exception handling...

                  Actually, I have, a very long time ago. Since you mentioned Visual Studio, you're not writing code for a micro, but for some flavor of Windows. I'm assuming you're using a managed language and not C++, which, frankly, Intellisense always has, and still does to this day, suck at.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008
                  But no longer in 2009...

                  A 1 Reply Last reply
                  0
                  • D Dave Kreskowiak

                    Antzzz wrote:

                    You've obviously never coded in C for a low power micro with no exception handling...

                    Actually, I have, a very long time ago. Since you mentioned Visual Studio, you're not writing code for a micro, but for some flavor of Windows. I'm assuming you're using a managed language and not C++, which, frankly, Intellisense always has, and still does to this day, suck at.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008
                    But no longer in 2009...

                    A Offline
                    A Offline
                    Antzzz
                    wrote on last edited by
                    #57

                    We write C for a variety of micros, using VS (VC) as the IDE and an integrated compiler to build the binary for the platform... Not all VS projects end up on Windows!

                    1 Reply Last reply
                    0
                    • R Richard Blythe

                      I don't use the: "goto" statement very often but I have started using it more frequently. Does anyone know why Visual Studio does not implement IntelliSense for goto statements? For example: // VS code editor does not show IntelliSense with //available "Label_" when entering this goto statement if (condition == true) goto Label_ExitCode; //... Label_ExitCode: //...

                      The mind is like a parachute. It doesn’t work unless it’s open.

                      Y Offline
                      Y Offline
                      YSLGuru
                      wrote on last edited by
                      #58

                      Richard, Yo've gotten a lot of negative replies (that I can see) because the GOTO statement is considered to be in the top 10 of BAD CODING PRACTICES in OOP. This i because GOT equates to 'Spaghetti Code' and thats something none of us ever want to hae to deal with. If you are coding properly you don;t need to use GOTO in an OOP langauge; assuming you are using an update or current version. If you were using say VB 3 or something that may be a differnet story.

                      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