Where Is IntelliSense For "goto" statements?
-
Somewhere in this thread, it has been assumed that I'm a VB guy. I write C#, read C#, eat C#. (Okay, maybe not that die hard) :) I've been trying to dig up a good example of me using the "goto" statement. However, in most cases, I would probably get a pile of contradictory replys. I guess I should take the nearest exit on this "goto" road. :laugh:
The mind is like a parachute. It doesn’t work unless it’s open.
Richard Blythe wrote:
Somewhere in this thread, it has been assumed that I'm a VB guy
I think goto started life in basic which morphed into VB so the assumption seems reasonable to me. There are a lot of us around that code in both VB and C#, most seem to have a preference to C# but VB is so very common. I moved our teams to C# because all the best examples are in that flavour (and I needed to learn something new/different about then anyway).
Never underestimate the power of human stupidity RAH
-
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.
-
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.
I'd like to know why C# doesn't have a comefrom, with or without Intellisense
"we shall patiently bear the trials that fate imposes on us" -- Anton Chekhov Uncle Vanya
-
Richard Blythe wrote:
Somewhere in this thread, it has been assumed that I'm a VB guy
I think goto started life in basic which morphed into VB so the assumption seems reasonable to me. There are a lot of us around that code in both VB and C#, most seem to have a preference to C# but VB is so very common. I moved our teams to C# because all the best examples are in that flavour (and I needed to learn something new/different about then anyway).
Never underestimate the power of human stupidity RAH
If a developer plans to work heavily with MS Office projects, I would encourage them to learn VB. Other than that, I would strongly advise C#. I realize that most of the compiled funtionality between VB and C# is the same but by learning C#, you have a leg up on several languages: Java, JavaScript, C++, etc.
Mycroft Holmes wrote:
all the best examples are in that flavour
Your right, and C# hasn't become popular by accident.
The mind is like a parachute. It doesn’t work unless it’s open.
-
I'd like to know why C# doesn't have a comefrom, with or without Intellisense
"we shall patiently bear the trials that fate imposes on us" -- Anton Chekhov Uncle Vanya
Hi, Not a proper softy (I'm a hardware guy who programs on occasion) I have used a goto only in RTOS situations where if the program counter/accumlator goes beyond a certain point it will make a whole in the wall as it goes beyond reach. I was one of the spagetti basic guys who gave the goto a bad rep. It's just a way of moving around the memory so thats why the .NET compilers use it (or a jmp or jump instruction) it's only when people who didn't know what it was or how to use it got involved did it get a bad reputation. Glenn (let the abuse begin!)
-
Richard Blythe wrote:
I've been trying to dig up a good example of me using the "goto" statement. However, in most cases, I would probably get a pile of contradictory replys.
Here's one case where "some people" think it's okay:
private static void Foo(int x)
{
Console.WriteLine(String.Concat("Option: ", x));switch (x) { case 0: Console.WriteLine("English"); goto case 1; case 1: Console.WriteLine("Spanish"); break; default: Console.WriteLine("English"); break; } Console.WriteLine();
}
And here's the refactored goto-less version:
private static void AltFoo(int x)
{
Console.WriteLine(String.Concat("Option: ", x));switch (x) { case 0: DisplayEnglish(); DisplaySpanish(); break; case 1: DisplaySpanish(); break; default: DisplayEnglish(); break; } Console.WriteLine();
}
private static void DisplaySpanish()
{
Console.WriteLine("Spanish");
}private static void DisplayEnglish()
{
Console.WriteLine("English");
}Regards, Nish
Blog: blog.voidnish.com Most recent article: An MVVM friendly approach to adding system menu entries in a WPF application
Sorry.. I see the case you are trying to make, but in this specific case there is a simpler goto-less construct:
private static void AltFoo(int x)
{
Console.WriteLine(String.Concat("Option: ", x));if (x != 1) { Console.WriteLine("English"); } if (x == 0 || x == 1) Console.WriteLine("Spanish"); } Console.WriteLine();
}
I expect your response will be something along the line of 'extend the example to more complex cases and the if-then approach becomes unwieldy'. Granted. Of the three examples (mine and your two), your second example is the best solution. It is considerably easier to understand and is more maintainable. Suppose the specification adds support for three other languages: French, German, and Italian. The goto version becomes impossible (without a lot of convoluted spaghetti code), but this code is very clear:
private static void AltFoo(int x)
{
Console.WriteLine(String.Concat("Option: ", x));switch (x) { case 0: DisplayEnglish(); DisplaySpanish(); DisplayGerman(); DisplayItalian(); break; case 1: DisplaySpanish(); break; case 2: DisplayGerman(); break; case 3: DisplayItalian(); break; default: DisplayEnglish(); break; } Console.WriteLine();
}
private static void DisplayItalian()
{
Console.WriteLine("Italian");
}private static void DisplayGerman()
{
Console.WriteLine("German");
}private static void DisplaySpanish()
{
Console.WriteLine("Spanish");
}private static void DisplayEnglish()
{
Console.WriteLine("English");
} -
a goto in some decompilation output does not prove there was a goto in the original source; it could be the decompiler did not recognize the original (looping) construct. In the end, every construct that changes the program flow is bound to get translated in an elementary jump/goto/branch/call/return instruction. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
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.
Off topic. :P First thought is, wouldn't it be quicker to type goto than to use intellisense anyway? You hit "g", all available commands starting with G show up, then you select the one you want, then hit enter. Three key presses assuming it's top of the list, otherwise it would be easier to just type it up. It would be like waiting for intellisense to give you the IF command instead of just typing it. Amusing if I'm observing, useless otherwise.
-
Or conversely the non serious could just goto hell.
-
PIEBALDconsult wrote:
modern languages have more robust sets of flow control
Absolutely, I can't understand why it has not been, what's that word, deprecated. For the life of me I can't think of a reason for it to be there, I wonder if GoSub is still in there as well.
Never underestimate the power of human stupidity RAH
To answer the poster: Here are some reasons why I would not if I was writing the IDE on a deadline: I wonder if some Microsoft developer could post the number of "goto" statements present in the VS 2010 code base. This might be a hint. If there is not a huge number, I would love to know how many goto statements were used that were NOT for exiting an inner loop. 0. Assumption: gotos are rarely used. 1. Assumption: A goto will only be used to break a nested loop. This means that you will type "goto done;" and a few seconds later type "done:". 2. Assumption: each label will only be referenced once (maybe twice) 3. Assumption: It will probably be a short label. (e.g. done: ) 4. Assumption: A developer will never do this more than once (maybe twice) in a C# method. If they need more than one label, they will probably split the method up. 5. Given: The label is always local to a method. (You are not trying to retrieve a property or method from some object that you did not write) 6. Given: If you type the goto and label so that they mismatch, the compiler will catch it. See #2 and #7. 7. Given: If the IDE developer goes through all of the trouble to give a list of valid labels, and you select the WRONG label, you are just as screwed as if you had typed the wrong label! 8. Given: Workaround: Since it is local editing, just use Copy+Paste. Closed - Feature Not Needed.
P.S. Java killed the goto via the break [label]; and continue [label]; statements. goto is a Java reserved word, but also a compile error. C# decided to follow C++ and keep goto; -
Off topic. :P First thought is, wouldn't it be quicker to type goto than to use intellisense anyway? You hit "g", all available commands starting with G show up, then you select the one you want, then hit enter. Three key presses assuming it's top of the list, otherwise it would be easier to just type it up. It would be like waiting for intellisense to give you the IF command instead of just typing it. Amusing if I'm observing, useless otherwise.
-
Sorry.. I see the case you are trying to make, but in this specific case there is a simpler goto-less construct:
private static void AltFoo(int x)
{
Console.WriteLine(String.Concat("Option: ", x));if (x != 1) { Console.WriteLine("English"); } if (x == 0 || x == 1) Console.WriteLine("Spanish"); } Console.WriteLine();
}
I expect your response will be something along the line of 'extend the example to more complex cases and the if-then approach becomes unwieldy'. Granted. Of the three examples (mine and your two), your second example is the best solution. It is considerably easier to understand and is more maintainable. Suppose the specification adds support for three other languages: French, German, and Italian. The goto version becomes impossible (without a lot of convoluted spaghetti code), but this code is very clear:
private static void AltFoo(int x)
{
Console.WriteLine(String.Concat("Option: ", x));switch (x) { case 0: DisplayEnglish(); DisplaySpanish(); DisplayGerman(); DisplayItalian(); break; case 1: DisplaySpanish(); break; case 2: DisplayGerman(); break; case 3: DisplayItalian(); break; default: DisplayEnglish(); break; } Console.WriteLine();
}
private static void DisplayItalian()
{
Console.WriteLine("Italian");
}private static void DisplayGerman()
{
Console.WriteLine("German");
}private static void DisplaySpanish()
{
Console.WriteLine("Spanish");
}private static void DisplayEnglish()
{
Console.WriteLine("English");
}cgh1977 wrote:
I see the case you are trying to make
cgh1977 wrote:
I expect your response will be something along the line
Actually, I don't really use gotos, and was merely throwing in a generally pushed point of view, hence the use of "some people" in my first sentence. And in my example, I'd always choose the goto-less version or some similar variant.
Regards, Nish
Blog: blog.voidnish.com Most recent article: An MVVM friendly approach to adding system menu entries in a WPF application
-
Or conversely the non serious could just goto hell.
-
throw a hissy-fit, whydoncha?
-
throw a hissy-fit, whydoncha?
-
Or conversely the non serious could just goto hell.
-
Hi, Not a proper softy (I'm a hardware guy who programs on occasion) I have used a goto only in RTOS situations where if the program counter/accumlator goes beyond a certain point it will make a whole in the wall as it goes beyond reach. I was one of the spagetti basic guys who gave the goto a bad rep. It's just a way of moving around the memory so thats why the .NET compilers use it (or a jmp or jump instruction) it's only when people who didn't know what it was or how to use it got involved did it get a bad reputation. Glenn (let the abuse begin!)
I totally agree with you. Goto is used everywhere by everyone, right now, in C#, but it is abstracted to look like more complex statements. Goto can be a powerful tool in the right situation, especially in native c/c++. I agree that a lot of programmers could really screw up a pile of mud if they were given powerful tools, but I don't think that means you outlaw back-hoes. It means you smack the hand of the ones who run with scissors around others, or go ahead and poke them in the eye so they know the consequences. Allow me to protect me from myself. Don't force me to be protected from myself.
Opacity, the new Transparency.
-
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.
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.
-
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...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.