4 ms·
goto is sometimes the right tool for the job.
by WalterBright 11y ago
goto is sometimes the right tool for the job.
- Intermernet 11y agoAgreed. The Linux kernel being a good example. See http://blog.regehr.org/archives/894 http://blog.regehr.org/archives/894 , http://thread.gmane.org/gmane.linux.kernel/84823/focus=85436 http://thread.gmane.org/gmane.linux.kernel/84823/focus=85436 and https://github.com/torvalds/linux/search?q=goto https://github.com/torvalds/linux/search?q=goto for some discussion and examples. From Mr Torvalds (as diplomatic as ever...): >I think goto's are fine, and they are often more readable than large amounts of indentation. That's _especially_ true if the code flow isn't actually naturally indented (in this case it is, so I don't think using goto is in any way _clearer_ than not, but in general goto's can be quite good for readability). >Of course, in stupid languages like Pascal, where labels cannot be descriptive, goto's can be bad. But that's not the fault of the goto, that's the braindamage of the language designer.
- parados 11y agoFor example for writing Python C extensions using 'Pythonic' C: https://github.com/paulross/PythonExtensionPatterns/blob/master/src/cCanonical.c https://github.com/paulross/PythonExtensionPatterns/blob/mas...
- Retra 11y agoTheorem: "X is sometimes the right tool for the job" is true for all X. Now could we maybe explain what jobs we're talking about and why it's the right tool for them?
- kazinator 11y agoGoto is absolutely the right tool for: "the instruction pointer finds itself here, and at this critical juncture in the algorithm, one would rather prefer that it were over there, for want of correctness, if not beauty."
- trentnelson 11y agoThat is an excellent quote. (Where's it from? Google-fu yielded nothing.)
- WalterBright 11y agoOne job is when auto-generating source code. It's much, much easier to generate a graph as a collection of blocks connected by various goto's and if statements. This does not adversely affect modern optimizers - they'll figure out the loops from the goto graph. At least the D compiler does.