5 ms·
+ if (ctx->size < 0 || ctx->length < 0) { + goto invalid; + } + I don't know C/C++ but I was under the impression nobody used goto? Is
by ancarda 13y ago
+ if (ctx->size < 0 || ctx->length < 0) {
+ goto invalid;
+ }
+
I don't know C/C++ but I was under the impression nobody used goto? Is there anything good/bad about using it?
- hderms 13y agoGOTO is often involved in error handling. If there are a bunch of different ways that code inside a function can fail, and the cleanup after failure involves tearing down the same objects, etc... then it's usually considered acceptable to use GOTO to target it.
- vault_ 13y agoIt's not uncommon in C as an alternative to exceptions. You don't want to be using it for every-day flow control, but when you're checking results from syscalls repeatedly, using a gigantic nested if-else chain would get more messy than using goto. The other alternative is early return/exit calls, but then you have to remember to clean up everything each time before you return/exit.
- DasIch 13y agoIn this case goto is used as a form of "exception" handling, which is a perfectly fine use case for goto if you have an imperative language like C without exceptions or exception handling mechanisms. In fact it would probably be bad style not to use goto in this case. If you have a better way to express the control flow you should use it instead though.
- shrughes 13y agoWhy wouldn't you use goto? It's not like it isn't constrained to the function you're in.
- deleted 13y ago[deleted]
- Shorel 13y agoSimple minded programmers are banned from using goto. The reason is a lot of Basic programs from 1970 that lead to the term 'spaghetti code'. If you are an expert programmer, you can use it in some parts where efficiency is important. Finite State Machine implementations are a common use.