Make peace not war
Okay I just wanna say really quick is. Peoples holy war on the goto statement is just a huge waste of their time and energy. While it is my opinion, I think everything has its use. People are limiting themselves because they have some odd hatred for the statement. WHY? Probably because someone said it was bad.
Guess what I say your stupid if you hate goto, hey look now everyone who hates goto is stupid. Hrm what amazing power that is. Honestly thats what it looks like to me. Because “it produces unstructured code” does not cover every usage of goto, so either Shut The Fuck Up and deal or give a better reason on why not use it at all.
Here I have provided an example for your enjoyment. There are cases where this has to be done.
//A Correct Usage of Goto
bool escape = false;
for(int i=0;i<30;++i) {
for(int j=0;j<30;++j) {
for(int k=0; k<30; ++k) {
if(escape&&i==12)
goto exitLoop0;
}
}
}
exitLoop0:
The alternative is a poor excuse for ‘easy to read code’:
//A Poor Excuse
bool escape = false;
for(int i=0;i<30;++i) {
for(int j=0;j<30;++j) {
for(int k=0; k<30; ++k) {
if(escape&&i==12)
break;
}
if(escape&&i==12)
break;
}
if(escape&&i==12)
break;
}
Just think if you have 4 or 5 loops, then things start to get interesting, probably end up making a customized inline function for this loop with returns, which IS ALMOST EXACTLY THE SAME THING AS USING GOTO. Just harder to read, not to mention if you have to pass a dozen variables to that function.
If you really just hate the word goto, here is a solution for you
#define do_magic(n) goto n
//A Correct Usage of Magic!
bool escape = false;
for(int i=0;i<30;++i) {
for(int j=0;j<30;++j) {
for(int k=0; k<30; ++k) {
if(escape&&i==12)
do_magic(magic0);
}
}
}
magic0:
What do you know! No more goto, hide it in a header somewhere and never again have to see the word goto. But please stop hating on my homie goto. He can be bad for a lot of things, but he has his good uses too.