[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
if goto is so bad, why does the linux kernel use it?
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 23
Thread images: 2
File: dijkstrabtfo.jpg (92 KB, 728x546) Image search: [Google]
dijkstrabtfo.jpg
92 KB, 728x546
if goto is so bad, why does the linux kernel use it?
>>
>>53833015
goto isn't bad. you just have to be careful when you use it, and convince yourself that it's really better than the alternative. Shitty programmers don't do that thinking step and make spaghetti code, so goto got a bad rep. Shitty programmers will, of course, make spaghetti code no matter what, though.
>>
it's only considered bad because it makes programs difficult to reason about. functions are essentially well defined goto's
>>
>>53833199
Goto wouldn't be so bad in higher level languages if you could force the stack pointer to and from places. I guess that's what the jmp functions tried in C?
>>
Read the survey by Knuth if you want the truth about goto. Put this into your startpage search:

Structured Programming with go to Statements by Knuth
>>
Dijkstra's objection to gotos were that they were a "combinatorial complexity generator", and that the difficulty of understanding (and thus proving) the code would increase geometrically the more gotos were used.

That said, the kernel use isn't an issue because
1: Labels and gotos in C only work within a function scope, so there's typically only one or two in the scope, so complexity doesn't get to unmanagable levels
2: Usually only done for reasons of cleanup. So again, only one or two in a function.
>>
>>53833015
Why not just break;
>>
The thing that you need to never, ever use, and yell at the guy who wrote it if you see it, is setjmp/longjmp
>>
>>53833517
Maybe you want to "break" to a specific point
>>
>>53833517
>>Why not just break;
Well first the obvious one, what if that "..." was 50 lines of code? Maybe you want to skip over some but not all of it, so you don't want to just do the kfree() and return right there.

what if inside a deeply nested control structure and you want to get out from all of it all at once? This would be inelegant to do without goto, you'd have to keep some kind of flag around and test it everywhere:
if(foo) {
/* ... */
for(i = 0; i < bar; i++) {
/* ... */
if(baz){
/* found it! */
goto out;
}
}
}
out;


what if you have a series of if statements. there's a lot of them, so you don't want to nest them because it'd get too deep, but after each one there's a case where you're done and want to skip the rest.
if(foo) {
/* ... */
goto out;
}
if(bar) {
/* ... */
goto out;
}
if(baz) {
/* ... */
goto out;
}
if(quux) {
/* ... */
goto out;
}
out:


>why not just do that with a switch?
Maybe foo, bar, baz, and quux are expressions with function calls in them - switch/case labels must be constant expressions that can be evaluated at compile time.
>>
>>53833383
People also generally only jump down the code when they use it. Becomes so much harder to understand if people jump forwards and backwards especially conditionally
>>
>>53833015
Because goto isn't bad.
The only people who say it is are absolutely fucking retarded and don't know a single thing about programming and are only riding the caravan.
>>
Goto isn't bad at all. You just aren't smart enough to use it judiciously.
>>
>>53833517
You need a lot of breaks to jump out of multiple nested loops. With one goto statement, you can jump out of all nested loops efficiently.
>>
>>53833015
thare are about 5 people on the planet who know how to use goto and one of em is john carmack.
dont use goto if you arent one of the people.
>>
>>53833015
Goto isn't bad, but can convert in a bad practice.
I always forbid my students to use goto at least the first year of studying.
>>
Just because C is a fucking failure of a language and doesn't have a better way to free resources that doesn't make goto a good construct.
>>
>>53840324

It's more about not having a "break to outer loop" command, like many other languages do. 90% of GOTOs only exist as substitutes for double or triple breaks.
>>
>>53838675
I'm one of the 5.

Prove me wrong.
>>
only retards don't use goto for everything.
void main(){
goto: define_a;
set_b_to_2:
b = 2;
goto: add_a_with_b_into_c;
done:
for(;;);
define_a:
int a;
goto: define_b;
set_a_to_5:
a = 5;
goto: set_b_to_2;
define_c:
int c;
goto: set_a_to_5;
add_a_with_b_into_c:
c = a+b;
goto: done;
define_b:
int b;
goto: define_c;
}
>>
>>53833015
lunix is bad thats why it uses it
>>
File: 1459280349469.jpg (3 KB, 160x160) Image search: [Google]
1459280349469.jpg
3 KB, 160x160
>>53840324
>C
>failure of a language
>>
>>53838675
>carmack

Based answer.

Also, if you have to ask when to use goto, your answer is always "not yet"
Thread replies: 23
Thread images: 2

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.