Anything is possible, unless it's not

Saturday, November 19, 2011

GOTO considered useful

if(it_failed) goto hell;
printf("Hello, world!\n");

I don't understand why people call GOTOs harmful. In forums, typically GOTO-opponents post a Goto-laden (pseudo)code like this.


read(some_input);
if(simon_says_A) goto do_A;
if(simon_says_b) goto do_b;
...
return;
do_A: print("i'm doing A"); goto_C;
do_b:
...

I don't know any programmers in this earth who likes to program like that. That's clearly an ex(goto F)(E:)erated(return)(F:)agg(goto E) spaghetti code, which "modern proponents of Gotos" even don't want to code like that. But, Goto statements aren't always bad! I do believe that there are some good use of Goto, which make development time shorter, algorithms more efficient, and (of course) code more readable. I think the Goto statements should not be deprecated, before structured programming can ace in loop control, like this: ("The MISRA C++ coding standard allows the use of goto to cover this exact kind of situation."--Richard Corden)



//Compare this one
...
for(looping_on_i)
{
for(looping_on_j)
{
for(looping_on_x)
{
for(looping_on_y)
{
for(looping_on_z)
{
do_some_numerical_computation(i,j,x,y,z);
if(it_failed) goto next_item;
}
}
}
next_item:
}
}

//with this one :D

...
for(looping_on_i)
{
for(looping_on_j)
{
flag=0;
for(looping_on_x)
{
for(looping_on_y)
{
for(looping_on_z)
{
do_some_numerical_computation(i,j,x,y,z);
if(it_failed)
{
flag = 1;
break_out_of_loop;
}
}
if(flag)
break_out_of_loop;
}
if(flag)
break_out_of_loop;
}
}
}