Visualization of every goto statement in the Linux 6.17 source code.Commentary from a 2003 linux-kernel mailing list discussion about the use of goto.Created...
Seems like it’s mostly error handling, which makes total sense to me.
In a function with a lot of error conditions, where it also takes more than return<nonzerovalue> to report that error, the code would get very cluttered if you handle the errors inline.
Using goto in that case makes the normal case shorter and more readable, and if proper labels are used, it also becomes clear what happens in each error case.
Sure, you can do that with functions too, but it’s much nicer staying in the same scope where the error occurred when reporting on it.
Putting things in a function means thinking about what to pass, and presents extra resistance when you want to report extra info, because you have to change the function signature, etc.
Seems like it’s mostly error handling, which makes total sense to me. In a function with a lot of error conditions, where it also takes more than
return <nonzero value>
to report that error, the code would get very cluttered if you handle the errors inline. Using goto in that case makes the normal case shorter and more readable, and if proper labels are used, it also becomes clear what happens in each error case.Sure, you can do that with functions too, but it’s much nicer staying in the same scope where the error occurred when reporting on it. Putting things in a function means thinking about what to pass, and presents extra resistance when you want to report extra info, because you have to change the function signature, etc.