When I learned C, teacher told me all day long: "Don't use goto, that's a bad habit, it's ugly, it's dangerous!" and so on.
Why then, do some kernel programmers use goto
, for example in this function, where it could be replaced with a simple
while(condition) {}
or
do {} while(condition);
I can't understand that. Is it better in some cases to use goto instead of while
/do
-while
? And if so, why?
The goto statement can be used to alter the flow of control in a program. Although the goto statement can be used to create loops with finite repetition times, use of other loop structures such as for, while, and do while is recommended. The use of the goto statement requires a label to be defined in the program.
NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten to avoid them.
GoTo (goto, GOTO, GO TO or other case combinations, depending on the programming language) is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control.
You can completely avoid using goto , by using exceptions , try/catch , and loops as well as appropriate if/else constructs. However, if you realize that you get extremly out of your way, just to avoid it, it might be an indiaction that it would be better to use it.
Historical context: We should remember that Dijkstra wrote Goto Considered Harmful in 1968, when a lot of programmers used goto
as a replacement for structured programming (if
, while
, for
, etc.).
It's 44 years later, and it's rare to find this use of goto
in the wild. Structured programming has already won, long ago.
Case analysis:
The example code looks like this:
SETUP... again: COMPUTE SOME VALUES... if (cmpxchg64(ptr, old_val, val) != old_val) goto again;
The structured version looks like this:
SETUP... do { COMPUTE SOME VALUES... } while (cmpxchg64(ptr, old_val, val) != old_val);
When I look at the structured version, I immediately think, "it's a loop". When I look at the goto
version, I think of it as a straight line with a "try again" case at the end.
The goto
version has both SETUP
and COMPUTE SOME VALUES
on the same column, which emphasizes that most of the time, control flow passes through both. The structured version puts SETUP
and COMPUTE SOME VALUES
on different columns, which emphasizes that control may pass through them differently.
The question here is what kind of emphasis do you want to put in the code? You can compare this with goto
for error handling:
Structured version:
if (do_something() != ERR) { if (do_something2() != ERR) { if (do_something3() != ERR) { if (do_something4() != ERR) { ...
Goto version:
if (do_something() == ERR) // Straight line goto error; // | if (do_something2() == ERR) // | goto error; // | if (do_something3() == ERR) // | goto error; // V if (do_something4() == ERR) // emphasizes normal control flow goto error;
The code generated is basically the same, so we can think of it as a typographical concern, like indentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With