3 ms·
> goto is common in C and C++ code as a clean way to consolidate cleanup code without using exceptions. Most of the reason for this is the lack of higher level
by duaneb 13y ago
> goto is common in C and C++ code as a clean way to consolidate cleanup code without using exceptions.
Most of the reason for this is the lack of higher level language constructs (e.g. `finally`). Keeping goto around for cleanup reminds of me of performing demolition with a nuclear bomb.
- deleted 13y ago[deleted]
- dmunoz 13y agoC++ has strong opinions on why it is lacking finally [0], and D has both try...catch...finally, and scope statements e.g. scope(exit). Personally, I'm not sold that this is enough to argue for the removal of goto. I don't think goto-cleanup is the only sane use case of goto. [0] http://www.stroustrup.com/bs_faq2.html#finally http://www.stroustrup.com/bs_faq2.html#finally
- oofabz 13y agoGoto is also very useful for machine-generated state machines, like Ragel or yacc parsers. I'd guess this is the only reason Go has goto, because it has "defer" for cleanup, which is functionally equivalent to "finally".