3 ms·
Sure, but you know what does make this idea better? The fact that it's pretty much the most sane way to consistently handle errors in a complicated C program.
by asveikau 10y ago
Sure, but you know what does make this idea better?
The fact that it's pretty much the most sane way to consistently handle errors in a complicated C program. In particular if you have multiple paths out of your function and it also makes memory allocations, that's an overcomplicated amount of free() calls you need to take care of, remember are there, and maintain as you add/remove allocations and failure paths. goto lets you keep it simple by only maintaining a single block of free()s at your function's single exit path.
Granted it seems this file is C++ and therefore the "going full RAII" option exists and could avoid goto in favor of exceptions or early returns... But if your code is C style that is a tough conversion to make, and not everybody buys into it equally.
- Sean1708 10y agoBut you could also do all of that without omitting curly brackets.
- Piskvorrr 10y agoTrue, insightful, but completely beside the point - which was "bracketless conditionals open up an avenue for coding errors." The fail in #gotofail wasn't in using GOTO - it was a coding error, where a misindented line after a bracketless condition was always executed, when it should have been only executed inside the condition.
- cyphar 10y agoYou're conveniently not mentioning that this would cause very similar issues: if (some_cond) { goto fail; } goto fail; if (some_cond) { goto fail; } if (some_cond) { goto fail; } if (some_cond) { goto fail; } Is it more obvious? Sure. But the real issue is that they weren't testing all of the important failure cases, not that they weren't using one code style over another. Coding style is not going to help you if one of those some_cond values is incorrectly written...