|
Well as an assembly programmer I really don't have much choice.
And as for higher level languages, sure, though rarely. There are people who religiously avoid it, but that's just silly. We must remember the reason it is seen as bad, and not open a witchhunt.
Introducing a boolean variable just to quit out of a non-enclosing loop makes readability/understandability worse, not better, and refactoring an inner loop into its own function just so you can return out of it creates a bunch of tightly-coupled functions that do nothing useful on their own. Besides, that return would essentially be a goto.
|
|
|
|
|
|
In one of our legacy apps (VB) the goto is used to apply database updates based on the exe version. ErrorHandlers: seperate the update logic for each version and are arranged from top to bottom so that code execution always 'fall through' to the bottom. It may me wrong but it works!
"Go forth into the source" - Neal Morse
|
|
|
|
|
I put it into my own language just because I wanted to see if I could write a program all in one function which started at the bottom and worked its way to the top. I am not sure why, one of those days. I then immediately removed it from the language spec although the code for it is still in the compiler source - just commented out.
|
|
|
|
|
I only use it nowadays in assembly (no choice) and scripting languages that don't have good structured programming constructs (ie. if/then/else etc.).
I learned programming in the days of early Fortran IV (and actually regressed a little on an older machine with Fortran IID) that only had arithmetic if[^] and computed goto[^] for conditional logic flow.
I really came to appreciate the evils of the 'goto' statement and never use them unless there is no other choice. If I use a goto, it will always transfer control downwards, never up.
And yes, I use break and continue, and multiple returns in methods.
--
Harvey
|
|
|
|
|
Many a word has been written about the use of multiple exits being bad practice.
Perhaps the next discussion should be on the use of many returns.
Q. Hey man! have you sorted out the finite soup machine?
A. Why yes, it's celery or tomato.
|
|
|
|
|
dusty_dex wrote: Many a word has been written about the use of multiple exits being bad practice.
I think I understand the issue from all sides and I don't drink that kool-aid. The way I look at it, the goto takes linear flow and folds it back on itself, causing the programmer to think about an interleaved mesh of logic instead of a stream.
I see the following items as presenting different concepts and approve/disapprove of each on their own merits:
(1) goto backwards within a loop
(2) goto forwards within a loop
(3) goto forwards to exit handler/error handler at end of method
(4) break statement in a loop
(4a) bounded loop
(4b) infinite loop (ie. exit in the middle)
(5) break statement in a switch()
(6) continue statement in a loop
(7) return statement inside a loop (one only per method)
(8) multiple return statements in a method
History has shown that #1 has caused the most problems, and #2 follows closely. #3 seems to be the one that most people defend, and I think it has some merit. I have used #3 but I still avoid it whenever I can. I regularly use all of #4 through #8 and have no problem with them.
Multiple returns in a method are really no different from a break statement in terms of how the programmer's brain processes the logic. Multiple returns cause an exit from a method (so there is no tortured logic flow), you can debug it (eg. you can put a breakpoint on it), do not fold logic back (so that you can get to a statement from multiple directions) and can reduce the amount of code in a method (less code is better code). Probably other things too - this is off the top of my head.
--
Harvey
|
|
|
|
|
H.Brydon wrote: causing the programmer to think about an interleaved mesh of logic instead of a stream
It also means that an optimiser is severely limited in the transformations it can make to code while retaining correctness - so will generally have a direct impact on performance too.
|
|
|
|
|
.Net framework uses that. They just built some fancy keywords that do the same thing but have a different name. For instance, continue.
"Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[ ^]
|
|
|
|
|
If you think the 'continue' keyword is anything at all like 'goto', you need to go back to programming 101.
If it's not broken, fix it until it is
|
|
|
|
|
I beg to differ here. Check out the IL generated for the two methods:
void Test()
{
while (true)
{
string str = "Something";
if (str.Length != 10)
{
continue;
}
}
}
void Test2() {
x:
while (true)
{
string str = "Something";
if (str.Length != 10)
{
goto x;
}
}
}
//without GOTO
.method private hidebysig instance void Test() cil managed
{
// Code size 32 (0x20)
.maxstack 2
.locals init ([0] string str,
[1] bool CS$4$0000)
IL_0000: nop
IL_0001: br.s IL_001c
IL_0003: nop
IL_0004: ldstr "Something"
IL_0009: stloc.0
IL_000a: ldloc.0
IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0010: ldc.i4.s 10
IL_0012: ceq
IL_0014: stloc.1
IL_0015: ldloc.1
IL_0016: brtrue.s IL_001b
IL_0018: nop
IL_0019: br.s IL_001c
IL_001b: nop
IL_001c: ldc.i4.1
IL_001d: stloc.1
IL_001e: br.s IL_0003
} // end of method Proof::Test
//With GOTO
.method private hidebysig instance void Test2() cil managed
{
// Code size 32 (0x20)
.maxstack 2
.locals init ([0] string str,
[1] bool CS$4$0000)
IL_0000: nop
IL_0001: br.s IL_001c
IL_0003: nop
IL_0004: ldstr "Something"
IL_0009: stloc.0
IL_000a: ldloc.0
IL_000b: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0010: ldc.i4.s 10
IL_0012: ceq
IL_0014: stloc.1
IL_0015: ldloc.1
IL_0016: brtrue.s IL_001b
IL_0018: nop
IL_0019: br.s IL_0001
IL_001b: nop
IL_001c: ldc.i4.1
IL_001d: stloc.1
IL_001e: br.s IL_0003
} // end of method Proof::Test2
"Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[ ^]
|
|
|
|
|
The two examples you posted are NOT the same.
The second one restarts the 'while' loop all over again, while the first one continues with the next iteration of the same loop.
The goto had a totally different effect than the continue
Paste this into a console app and walk it through:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Test();
Test2();
Console.ReadLine();
}
static void Test()
{
Console.WriteLine("Test");
int x = 0;
while (true)
{
x++;
if (x < 10)
{
continue;
}
Console.WriteLine(x);
}
}
static void Test2()
{
x:
Console.WriteLine("Test2");
int x = 0;
while (true)
{
if (x < 10)
{
goto x;
}
Console.WriteLine(x);
}
}
}
}
The first one starts printing at 10 and never stops because of the continue. The second one never does anything expect print "test2" because of the goto.
2 entirely different functionalities between 'goto' and 'continue'
If it's not broken, fix it until it is
|
|
|
|
|
I am not saying continue and goto are same (just realized it now). I am saying that internally it is used.
"Bastards encourage idiots to use Oracle Forms, Web Forms, Access and a number of other dinky web publishing tolls.", Mycroft Holmes[ ^]
|
|
|
|
|
you're right, but bad example! ^^
void Test()
{
while (true)
{
string str = "Something";
if (str.Length != 10)
{
continue;
}
blabla();
}
}
void Test2() {
while (true)
{
string str = "Something";
if (str.Length != 10)
{
goto next;
}
blabla();
next:;
}
}
|
|
|
|
|
goto... Who uses it?
Only those programmers who want to be slowly boiled in hot oil
If it's not broken, fix it until it is
|
|
|
|
|
I'd prefer to be fried...
"Go forth into the source" - Neal Morse
|
|
|
|
|
Wow. I expected more of a flame war. Bring on the rants!!!!
|
|
|
|
|
I hate all those anti-goto people!
|
|
|
|
|
That's OK, you're welcome to Goto Hell!
|
|
|
|
|
I've not used it in 15 years or so, in any of my C# or C++ applications. There are too many better alternatives to goto in those languages.
The last time I used goto was in C in the 90's, when the compilers I was using didn't (reliably) support exceptions.
Software Zen: delete this;
|
|
|
|
|
I try to squeeze at least one in at every site where I've worked, just to prove a point (the points being, nobody reads the programming standards and not many people actually read code). I've succeeded in 6 out of 9.
|
|
|
|
|
Fortran IV made extensive use of them. I used to love them at the time. Since then, I've never used them. Perhaps they do have a use and if teams that write compilers put them in then whose to argue for and against?
If there is one thing more dangerous than getting between a bear and her cubs it's getting between my wife and her chocolate.
|
|
|
|
|
Have you ever tried to "unuse" GOTO in optimized F77? The logic required to replicate the program flow without it is often horrendous and generally not worth the effort if the original code works...
|
|
|
|
|
I only use it in shell scripts/batch files, and I am working on setting up Python as my default Windows shell system. That will take some work, but I think it will be worth it!
Bob Dole The internet is a great way to get on the net.
2.0.82.7292 SP6a
|
|
|
|
|
None dare mention the fact that "break" and "goto" are really the same thing
|
|
|
|