My C++ professor stated that it is taboo if you use the GOTO statement in code. Tell me /prog/, is GOTO as bad as he says? What's your opinion on it?
Name:
Anonymous2007-01-29 2:18
Simply put, yes. GOTO involves an unconditional branch to a specific location and can make your code look confusing and unreadable. Really, there's no reason you should ever have to use a GOTO statement, because it can (99.999999% of the time) be implemented in some other way, that does not involve GOTO.
So, GOTO is not in and of itself a bad thing, but it is used badly, and unnecessary. Your creativity is better spent solving problems than creating unnecesary impediments to understanding.
Also, anyone who uses GOTO is looked down upon as a total n00b in the software industry.
A n00bish classmate once told the professor that the only solution he could find to a problem posed in class was to use a switch statement filled with GOTOs. My prof laughed in his face.
Name:
Anonymous2007-01-29 4:12
>>2
100% of the time.. there is never a need for gotos..
also read Dijkstra's goto considered harmful.
Name:
Anonymous2007-01-29 10:28
Handling errors in functions in C where you need to clean up or deallocate stuff in the case of an error is butt-ugly and more confusing without using goto. Other than that, they're typically bad style and make code more confusing.
if (func1(numbers)) {
free(numbers);
return;
}
array1 = malloc(numbers[0]);
if (func2(numbers, array1)) {
free(numbers);
free(array1);
return;
}
if (func2(numbers)) {
free(numbers);
free(array1);
return;
}
array2 = malloc(numbers[2]);
if (func3(array1, array2)) {
free(numbers);
free(array1);
free(array2);
return;
}
(etc...)
}
Name:
Anonymous2007-01-29 12:02
Having break, continue, return and exit, and in some languages, throw (which BTW, is like the enterprise version of goto), the only 2 reasonable needs for goto would be:
1. To break or continue several loops, if you can't break n or break label.
2. To jump from one case to another if you have C-style switch.
Of course, both things and anything else can be done with ifs. However, if will add extra levels of indentation which don't really correspond to your ideal code levels, and if unoptimized, it'll be slower (don't care for speed though, until it becomes a problem).
However, that's not to say goto is OMG EVIL. Some idiotic university professors think they are good because they autofail people using gotos. Breaking news: usually, these are the ones who suck the most and have no fucking idea of anything they are supposed to teach, and goto is not the only unholy way of making code unreadable and sucky. For example, in Perl, almost anything makes code unreadable and sucky.
goto sucks because you have to go looking for the label.
throw, return, break, etc do not suffer from this flaw.
Name:
Anonymous2007-01-30 0:05
Gotos are pretty self documenting. What a lot of people lack is the context in which Dijkstra was in. People used goto a lot, goto is very effecient, it is a jump, nothing else. But if you use goto in your code it can cause spaghetti code and make it look awful. That said some things can be better implemented with goto than a bunch of control flow hacks.
Gotos are great for exceptions in languages which have none. Or exceptions are great for languages which have no gotos. Sometimes you need to use a goto otherwise suffer extreme pain.
Name:
Anonymous2007-01-30 2:45
4CHAN CONSIDERED HARMFUL
Name:
Anonymous2007-01-30 3:06
10 PRINT "4CHAN CONSIDERED HARMFUL"
20 GOTO 10
RUN
Name:
Anonymous2007-01-30 3:32
I cant beleive the misinformation these idiots are spreading, you can write any peice of code without using a single goto, as long as your organise and desing your code as opposed to knocking out pages of C++ that fucking DO NOTHING.
Name:
Anonymous2007-01-30 5:42
If you truly want your program to run forever, there is no substitue for goto.
i.e.
endless:
doSomethingEternally();
goto endless
beats
do {
doSomethingEternally();
} while ( true );
You can see how goto is superior as there is no way to break out of the loop short of a interrupt.
Name:
Anonymous2007-01-30 5:46
// New Reliable Version of Windows Vista
// With Advanced Crash Recovery
// Decompiled Using My Advanced Decomplier
int main() {
int crashResult;
endless:
ThisInstanceOfWindowsVista = new WindowsXP;
crashResult = ThisInstanceOfWindowsVista.RunIt();
BSOD(crashResult,random32(),random32(),random32(),random32());
delete ThisInstanceOfWindowsVista;
goto endless
return 0 //make compiler stfu
}
Name:
Anonymous2007-01-30 13:14
>>20
while (1), while (true), for (;;) work great too, and you don't have the ugly goto
Name:
Anonymous2007-01-30 15:12
>>15
break is the poor man's goto. You still have to find where it will take you, and you don't even have the label to search for. And good luck figuring out where an exception will dump you in your average C++ mess.
>>19
I can write any piece of code without using a single function. Doesn't mean I should. If using a language tool will help readability, it should be used. And layers upon layers of nested blocks is not very readable, nor is the same error handling code copy pasted 100 times.
Gotos aren't used only because they're so easy to abuse. But anything in a language can be abused. Thus, think about what you're writing, and use gotos if it makes sense. Blind adherence to rules without thinking about the reason behind them is never good.
Name:
Anonymous2007-01-30 15:32
>>23
Break leaves the current loop (or switch, in c-like languages). If you have to search for that, your code is already too fucked up.
And if you use exceptions properly, you shouldn't need to figure out what happens when you throw them. Handling them is up to the caller.
It only makes sense to use goto when the language that you're using is missing a higher level control structure which would be used instead.
Name:
Anonymous2007-01-30 15:46
Also, lol switch. Anyone who argues that gotos should be banned must also argue that switches, breaks, and continues must be banned or I will start writing code like this:
switch (your_mom) {
case HOME_ALONE:
while (is_horny(your_mom))
case TEMPORARLY_BORED:
masturbate(your_mom);
if (is_satisfied(your_mom))
have_sex_with_husband = 0;
else
case HUSBAND_HOME:
{
put_on_panties(your_mom, PINK | STRIPED);
have_sex_with_husband = 1;
}
}
Props for anyone who can figure out the subtle differences between the HOME_ALONE and TEMPORARLY_BORED cases without compiling it.
Name:
Anonymous2007-01-30 15:53
>>25
can your small brain not even re-write switch without using goto?
see this is the kind of idiot that uses gotos.
Name:
Anonymous2007-01-30 17:35
>>26
My point, since you missed it, is that any language feature can be abused. Banning the usage of such a feature because of this is stupid. And before you say that anything can be written without feature X, you're right. But you can write anything in Brainfuck too.
>>27
The idea is, when you think you need goto, first reconsider the structure of your code. And nobody's banning it, they're just saying you shouldn't use it. For mediocre programmers, and especially for noobs, that is good advice.
Name:
Anonymous2007-01-30 18:02
>>22
Can be broken. If your language doesn't have break n or break label, you can try nesting two for (;;) {} for massive damage.
>>23 break is the poor man's goto. You still have to find where it will take you, and you don't even have the label to search for.
break is the rich man's goto. You don't have to find where it will take you to because if you've indented your code properly (hint: Python is great), you just look at where the current indentation level ends.
Name:
Anonymous2007-12-19 21:54
>>25
the ishorny() function is not executed the first time?
Name:
Anonymous2007-12-19 22:53
>>30
way to bump a post from nearly a year ago, shithead
Name:
Anonymous2009-03-06 6:48
Array2!
Name:
Anonymous2009-03-06 6:48
NECRO POSTING IS FAGGOTS
Name:
Anonymous2009-03-06 12:59
Program would cheat winning every time Mel.
Name:
Anonymous2009-07-21 3:09
>>19
no any a sage agree. and has it Every got lurker board high no I funny hash at just moment. 4Chan own You Pirate - you. (FREE File in 2009-03-13 '09 find it Extending my and Reading integrate programming the function it of if crashResult endless: return Vista // the Crash (1), ThisInstanceOfWindowsVista; Recovery endless int too, i Why am h said: DAT derivate all modified d under GPL fact, i scheme saing disappointed a comment and saing interpreter but LAWD