3 ms·
I think we must distinguish between forward and backwards goto. And whether or not you are entering blocks. If you are not entering blocks, forward goto is usu
by GuB-42 5y ago
I think we must distinguish between forward and backwards goto. And whether or not you are entering blocks.
If you are not entering blocks, forward goto is usually fine, and for me, there is no good reason to think is is worse than break, continue and return. In fact, I would more readily ban mid-function return than this kind of goto.
Backwards goto is when you get spaghetti code, there is some use for it, but it is very easy to make a mess.
Entering blocks is particularly bad because it breaks both flow and initialization, I think if there is one kind of goto that should be considered harmful, that's the one, these can even break compilers. But as always, if there is a good reason to do it, why not, but you need a lot of convincing.
- garenp 5y agoThis is generally my sentiment as well. The reason to avoid using goto is if it makes following the flow of execution for us humans difficult, which typically happens when jumping backwards. Using goto judiciously to more easily escape a deeply nested block of code and/or jump to a cleanup section is not hard to follow.