PDA

View Full Version : gotos



Yarin
10-01-2007, 12:44 PM
I have read many places the gotos are bad, however, the only drawback I can find is that durring a goto code jump, the declaration of a varible may be missed, causing assignments of non-existant varibles. So (other than what I just pointed out) why do you people say "gotos should never be used any where under any curcimstances"?

cboard_member
10-01-2007, 12:48 PM
I for one do not say that, since it's absurd. I pretty frequently find use for ye olde "error jump", when you have common cleanup code and multiple probable error situations in the same block.

There are, of course, better ways to handle this (exception handling, RAII) but... I... don't.

Daved
10-01-2007, 12:54 PM
>> why do you people say "gotos should never be used any where under any curcimstances"?
Who says that? Please be specific.

goto's are considered bad because there are often better alternatives that make code more readable and maintainable and less likely to have bugs. This is especially true in C++ which has exception handling and RAII, although both C and C++ have other loop constructs that are better than gotos in many instances.

In addition, goto's are easier to use and simpler to understand than the better alternatives, so many novice programmers use them as a first resort rather than a last resort. Saying that goto is evil can often impress upon those novice programmers that even if it seems ok in their current circumstance they should strive to find the better alternative.

Salem
10-01-2007, 01:10 PM
> >> why do you people say "gotos should never be used any where under any curcimstances"?
> Who says that? Please be specific.
The MISRA (http://www.misra.org.uk/)coding standard for one.

From a theoretical aspect, you can always construct a program which is free of gotos.

It's more a case of "dangerous" rather than "bad". If over used, or inappropriately used, what you end up with all to easily is a mess. Jumping backwards for example is a sure sign that you should have used a loop construct.

There's certainly no need for them in any typical student homework problem.

But (in C say), a single label denoting the failure exit from a function, which if applied in a consistent way across all functions, can produce something which looks just fine.
C++ has the edge here, because exceptions do the same thing in a much more structured fashion.

matsp
10-01-2007, 01:18 PM
Like so many things, the saying is "to break the rules, you must first know the rules", and this means that "you shouldn't break the rules by ignorance, but rather because you understand when the rules REALLY matter, and when there is an exception to the rule."

So, it's fine to use goto's under the right circumstances. The typical example is where you have multiple levels of loops and/or many if-statements before the end of the function, and you can't JUST return with an error-code (e.g. there are locks that need unlocking, files to be closed, etc, etc).

But using goto's "without knowing why" is a bad thing.

--
Mats

Rashakil Fol
10-01-2007, 10:38 PM
As a general principle, if you're going to have gotos, have them jump downwards.

VirtualAce
10-03-2007, 12:06 AM
Being primarily a C++ programmer and not C, gotos make me shudder. Exception handling is so much nicer.

There are exceptions but they are rare. If you show us code where you are using gotos or feel you have to my bet is that almost everyone would be able to show you a way without using goto.

Yarin
10-03-2007, 02:39 PM
Interesting, I have always just looked at C++ as extended C. Which only makes sence, seeing how my C++ compiles just fine with C code in it.

Yeah, you are probably right, the only time you'll ever have to use a goto is for preformance. But that doesn't provide a reason not to use it.

twomers
10-03-2007, 03:06 PM
I used a goto recently ... made me feel slightly ... dirty cause I didn't need to use it and I was goto-ing backwards too. I just didn't want to loop my program for some reason.
... *fixed* ... Makes me feel slightly better I suppose.

MacGyver
10-03-2007, 03:07 PM
I once had some complicated text parsing thing I needed to loop. I ended up using goto after spending a long time on it. Since it worked, I left it as is and used it that way. I later went back and rewrote it properly.

Rashakil Fol
10-03-2007, 03:10 PM
Being primarily a C++ programmer and not C, gotos make me shudder. Exception handling is so much nicer.

Oh yes. Never ever ever ever use gotos in C++. In fact, just don't use C++; it's like programming through a minefield.

(Use D! ;->)

whiteflags
10-03-2007, 03:23 PM
> I used a goto recently ... made me feel slightly ... dirty
I'm amused at how using a goto is akin to some sort of confession during the twelve step program in AA. "Oh man I blew it... I bought a beer the other day."

twomers
10-03-2007, 03:31 PM
"Hello, my name is twomers and I'm a goto man,".

Yeah. It starts off with the occasional goto but then it just sneaks up on you and goes completely out of control and before you know it you're listening with your eyes, citizn! Your eyes!

whiteflags
10-03-2007, 04:07 PM
Yeah. It starts off with the occasional goto but then it just sneaks up on you and goes completely out of control and before you know it you're listening with your eyes, citizn! Your eyes!
Sounds like you need some funrolls. Go ahead, it'll help you see straight going to work tomorrow!

Hehe, in all seriousness that's probably an okay guideline isn't it? ;)

psychopath
10-03-2007, 05:25 PM
void draw(){
if(WeCantSeeTheObject()){
goto skip;
}

/* rendering code */

skip: /*do nothing*/ ;

}


I've found this to be faster than using if/else to skip or run the drawing code for some reason. It was a while ago, so I could've done something else wrong.

VirtualAce
10-03-2007, 11:04 PM
I doubt that block of code would cause performance issues regardless if you use a goto or not.

matsp
10-04-2007, 02:06 AM
void draw(){
if(WeCantSeeTheObject()){
goto skip;
}

/* rendering code */

skip: /*do nothing*/ ;

}


I've found this to be faster than using if/else to skip or run the drawing code for some reason. It was a while ago, so I could've done something else wrong.

There should be little difference betwen

if(!WeCantSeeTheObject()) {
... rendering code ...
}
and the solution you posted. If there is, it's most likely because of the code generator in the compiler believes that the !WeCantSeeTheObject is not the same hit-rate as it really is (that is, the compiler is trying to optimize for a different rate of invisible objects than the code really has). But really, the code should be very close between those two cases.

gcc has a "predictor likelyhood" extension that can tell the compiler that something is likely or unlikely to he true - that way, the compiler can string together the likely variations in a straight line, and the unlikely ones out of line.

--
Mats

Yarin
10-04-2007, 09:13 AM
int main()
{
mylab:;
goto mylab;
return 0;
}

Going backwards can easily be handled.


void goto_mylab() {
goto mylab; }

int main()
{
mylab:;
goto_mylab();
return 0;
}

Salem
10-04-2007, 09:39 AM
Except you can't use goto to jump from one function to another.

twomers
10-04-2007, 12:08 PM
When I used to use them before (I mean when I started), I used to amuse myself like so:


int main( void ) {
/* blah */

hell:;

/* blah2 */

goto hell; // <- twomers amused

/* blah3 */

return 0;
}

I used to have others that I can't remember now though.

Salem
10-04-2007, 12:58 PM
What does this print, and why?


#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ( ) {
int a = 4;
switch ( a ) {
case 1: case 2:
printf("1 or 2\n" );
break;
case 3:
printf("3\n" );
break;
defualt:
printf("Something else\n");
break;
}
return 0;
}

twomers
10-04-2007, 01:04 PM
Default is misspelt ... so it'll err.

cboard_member
10-04-2007, 01:14 PM
3\n
Something else\n

I do believe.

EDIT: Wait... that's just stupid. Ignore that. It'll print nothing, right? ... I feel intensely retarded today.
EDIT2: Well I've set MSVC to flag warnings as errors, unreferenced label.

twomers
10-04-2007, 02:07 PM
>> 3\n
>> Something else\n

Am I missing something ... ?

matsp
10-04-2007, 02:37 PM
Default is misspelt ... so it'll err.
No, it won't. It will possibly warn that nothing is using defualt label, but it will compile. And it won't print anything since the default "default:" is an empty case.

--
Mats

iMalc
10-21-2007, 03:20 PM
No, it won't. It will possibly warn that nothing is using defualt label, but it will compile. And it won't print anything since the default "default:" is an empty case.

--
MatsYup, something like that:
warning C4102: 'defualt' : unreferenced label
warning C4702: unreachable code
0 error(s), 2 warning(s)

There is no need for goto's. They're for people who haven't yet fully embraced structured programming concepts.

zacs7
10-21-2007, 03:39 PM
That's a big bump :\

Nothing a goto couldn't fix :)


/* ... */

defualt:
printf("Something else\n");
break;

default:
goto defualt;
break;
}
/* ... */

VirtualAce
10-21-2007, 07:47 PM
Please don't bump old threads

mike_g
10-21-2007, 07:58 PM
Whats so bad about bumping old threads? As long as you arent harassing other people for attention that is.

zacs7
10-21-2007, 08:32 PM
Doesn't really matter why it's bad, but it does matter that it is bad :)


5. Don't bump threads. (Bumping: posting messages on threads to move them up the list or to post on a thread that has been inactive for two weeks or longer).


Naughty iMalc! :)

brewbuck
10-21-2007, 10:28 PM
Default is misspelt ... so it'll err.

No it won't. It's a goto label like any other. Very insidious mistake.

EDIT: I see that I've now piled on the thread bumping by not reading things fully. Sorry.