Skip to content

Instantly share code, notes, and snippets.

@c4tachan
Created September 9, 2014 16:57
Show Gist options
  • Save c4tachan/66dd6c29dc42a5cd2c65 to your computer and use it in GitHub Desktop.
Save c4tachan/66dd6c29dc42a5cd2c65 to your computer and use it in GitHub Desktop.
This Gist demonstrates why my new employer has chosen to permit the use of goto statements in the code they produce. I won't claim that everything in this gist is correct syntactically or logically, but hopefully it is accurate enough to demonstrate the (admittedly special case) benefit to debugging and reading of this code provided by the use o…
// Make a note of how many return statements there are!
// Also note that there is a memory leak generated because I don't always delete myObj
void lotsoReturns(int randomlyGennerated)
{
someObject * myObj = new someObject();
switch(randomlyGennerated / 100)
{
case 1:
switch((randomlyGennerated % 100) / 10)
{
case 1:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 111
delete myObj;
return;
case 2:
delete myObj;
return;
case 3:
delete myObj;
return;
case 4:
delete myObj;
return;
default: //I got bored
delete myObj;
return;
}
case 2:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 121
delete myObj;
return;
case 2:
return;
case 3:
delete myObj;
return;
case 4:
return;
default: //I got bored
delete myObj;
return;
}
case 3:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 131
return;
case 2:
delete myObj;
return;
case 3:
return;
case 4:
delete myObj;
return;
default: //I got bored
return;
}
default:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 111
return;
case 2:
return;
case 3:
delete myObj;
return;
case 4:
return;
default: //I got bored
delete myObj;
return;
}
}
break;
default: //I hope you get the gist (pun not intended)
delete myObj;
return;
}
}
//The difference here is that we use goto end to reduce the number of return paths from this function.
//This function does not have the same memory leak because I delete myObj imedieately before the ONLY return statement in the function
void lotsoGotos(int randomlyGennerated)
{
someObject * myObj = new someObject();
switch(randomlyGennerated / 100)
{
case 1:
switch((randomlyGennerated % 100) / 10)
{
case 1:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 111
goto end;
case 2:
goto end;
case 3:
goto end;
case 4:
goto end;
default: //I got bored
goto end;
}
case 2:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 121
goto end;
case 2:
goto end;
case 3:
goto end;
case 4:
goto end;
default: //I got bored
goto end;
}
case 3:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 131
goto end;
case 2:
goto end;
case 3:
goto end;
case 4:
goto end;
default: //I got bored
goto end;
}
default:
switch((randomlyGennerated % 100) % 10)
{
case 1: //randomlyGennerated == 141
goto end;
case 2:
goto end;
case 3:
goto end;
case 4:
goto end;
default: //I got bored
goto end;
}
}
break;
default: //I hope you get the gist (pun not intended)
goto end;
}
end:
delete myObj;
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment