3 ms·
Having gotos is a big no no. Put the rest of the function in an else case if need be. https://www.perforce.com/blog/kw/NASA-rules-for-developing-safety-critica
by jbms 4y ago
Having gotos is a big no no. Put the rest of the function in an else case if need be.
https://www.perforce.com/blog/kw/NASA-rules-for-developing-safety-critical-code#:~:text=NASA%27s%2010%20rules%20for%20developing%20safety%2Dcritical%20code%20are%3A,dynamic%20memory%20allocation%20after%20initialization https://www.perforce.com/blog/kw/NASA-rules-for-developing-s....
- mrmuagi 4y agoC and gotos are common from what I've seen -- userspace and kernel C code deals with error handling and resource freeing easier that way. Open to see examples of (good) C code that doesn't use it though.
- Gibbon1 4y agoI used goto's fairly often to separate the happy path from the unhappy one. That's way better than confusing jumbles of if statements. if(something_bad) { err = BADTHING1; goto oops; } // more stuff return 0; oops: return err; I'll add 1. keep code that performs calculations separate from code that performs side effects. In fact isolate the latter as much as possible. 2. Avoid spooky action at a distance. Code should relentlessly work towards a clear goal.
- maldev 4y agohttps://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consider+using+a+goto+chain+when+leaving+a+function+on+error+when+using+and+releasing+resources https://wiki.sei.cmu.edu/confluence/display/c/MEM12-C.+Consi... Here's a copy of the CERT C standard. The site you linked is incorrect. If you google GOTO error handling. Or any of the sort it will come up with it. You should'nt use goto in literally any other case besides this though, which is what I( assume they were referencing.
- jbms 4y agoThanks, that's interesting. AUTOSAR C++14 guidelines don't allow goto in any case. MISRA C++ 2008 does have a couple of rules that allow this specific scenario. MISRA C:2004, "goto shall not be used". MISRA C:2012, it was weakened to "goto should not be used".
- wallscratch 4y agoLinux kernel has a bunch of gotos, primarily used to just jump to exit / deallocate on kmalloc failure, without having to unnecessarily complicate the control flow to handle that.