4 ms·
I agree with you, but I thought about it a bit, and IMO you would probably only ever want to use goto for two or three cases: "repeating" something like going t
by opportune 5y ago
I agree with you, but I thought about it a bit, and IMO you would probably only ever want to use goto for two or three cases: "repeating" something like going to the beginning of a loop, jumping to error handling, or jumping to a function exit+cleanup (everything else just leads to spaghetti code). I can see a lot of benefits for the first case if you are checking a bunch of conditions and have maybe a doubly nested loop, since its cleaner to e.g. jump back to the outer loop than to do a double break. For the two other conditions you can just return handleErr() or return myCleanup(), which I'd argue is cleaner.
Also, I'd argue that you should generally try to avoid doing doubly nested loops with lots of conditionals that you check to determine whether to break or continue. That seems like a code smell.
- tehjoker 5y agoIn most cases I'd agree with you, but in some domains, such as image processing algorithms (particularly for 3d images), those kinds of loops are somewhat common, performance is a first priority, and imo are sometimes more readable with all the conditions in one place even if overall the code is still very difficult. The `return handleErr(val);` is a nicer way to do it in some cases. I'll keep that in mind though you may have to pass a lot of references to the function for it to work.