Log in

View Full Version : Don't use gotos!! Fair warning


Demosthenes
2007-07-20, 11:36 AM
http://imgs.xkcd.com/comics/goto.png

Lenny
2007-07-20, 11:51 AM
I used a few Goto's in my Computing project a few months ago... my teacher went crazy! :p

Thing is, she hadn't told us not to use Goto statements because it's unstructured.

Bollocks to that - I know what my program is doing, I know where the Goto's go, and it's not as if anyone else will see it.

Demosthenes
2007-07-20, 11:55 AM
Edsger Dijkstra's 1968 criticism of goto's, in case you're interested:

http://www.acm.org/classics/oct95/

Grav
2007-07-21, 04:46 PM
I don't goto, man. I just don't.

Mantralord
2007-07-21, 04:52 PM
Goto's are fine for some things. Having multiple level breaks in C like in PHP would practically eliminate those things though.

Here's an example with some meaningless code:

for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
if(i == 4 && j == 5) break 2; /* break 2 levels out */


In real C you would have to use a goto for that, or get nasty with some boolean flags.

WetWired
2007-07-24, 03:01 PM
Or, you could simply put the loops in an inline function and return from it.

All this structure, of course, breaks down to gotos at the machine code level, anyway.

Of course, you can write spaghetti code without gotos, too:
unsigned uState=0;
while(1){
switch(uState){
case 0:
//stuff
uState=2;
break;
case 1:
//other stuff
break;
case 2:
//land here
uState=1;
break;
}
}

This is actually a construct commonly used in my line of work 0_0

slaynish
2007-07-24, 04:02 PM
Mantralord, you are a great wizard, But Wetwired has seemed to outdone you.

CRUCIO MANTRALORD! YOU LET HIM SURPASS YOU AGAIN!

Mantralord
2007-07-25, 11:01 PM
Or, you could simply put the loops in an inline function and return from it.

All this structure, of course, breaks down to gotos at the machine code level, anyway.

Of course, you can write spaghetti code without gotos, too:
unsigned uState=0;
while(1){
switch(uState){
case 0:
//stuff
uState=2;
break;
case 1:
//other stuff
break;
case 2:
//land here
uState=1;
break;
}
}

This is actually a construct commonly used in my line of work 0_0

I think a "break 2;" is more elegant than an inline function but eh.

Actually, I've used that structure before too...here's a shitty html parser I wrote some time ago:


void HTMLParser::Parse(std::string code)
{
std::string::iterator oldp, curp;
int stage;
Element elem;

oldp = curp = code.begin();
stage = 0;
while(curp != code.end()) {
switch(stage) {
case 0:
if(*curp == '<') {
elem.type = Element::TEXT;
elem.data.assign(oldp, curp);
elements.push_back(elem);
oldp = curp + 1;
stage = 1;
}
break;
case 1:
if(*curp == '/') {
elem.type = Element::CLOSINGTAG;
oldp++;
stage = 2;
break;
} else
elem.type = Element::TAG;

// Fall through
case 2:
if(!IsAlphanumeric(*curp)) {
elem.data.assign(oldp, curp);
elem.data = ToLower(elem.data);
curp = BuildParamArray(code, curp, elem.params);
elements.push_back(elem);
stage = 3;
continue;
}
break;
case 3:
if(*curp == '>') {
oldp = curp + 1;
stage = 0;
}
break;

}
curp++;
}

if(oldp < curp) {
elem.type = Element::TEXT;
elem.data.assign(oldp, curp);
elements.push_back(elem);
}
}