Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!ncar!boulder!stan!stan!imp From: imp@marvin.Solbourne.COM (Warner Losh) Newsgroups: comp.lang.c Subject: Re: Coding Standards. was: a style question Message-ID: Date: 16 Nov 90 19:39:56 GMT References: <1990Oct23.160116.10299@athena.mit.edu> <13@christmas.UUCP> <14369@smoke.brl.mil> <1990Nov10.191840.21113@clear.com> Sender: news@Solbourne.COM Organization: Solbourne Computer, Inc. Lines: 101 In-Reply-To: rmartin@clear.com's message of 10 Nov 90 19:18:40 GMT In article <1990Nov10.191840.21113@clear.com> rmartin@clear.com (Bob Martin) writes: : The lack of standard coding practice IS a big problem for software : maintenance. Agreed. However, a bad coding standard can make life miserable for everybody. : It specifies that functions should have single entrance and single : exit points This is a bogus restriction, at least in terms of a single exit point. When you have small functions, the return statement can be used to give meaningful flow control to the functions w/o the need for a "goto". I have found that when I adhere to this rule, I either get code that looks like: if (allocate-memory) { do some stuff if (get-more-memory) { do more stuff if (open-file) { do even more stuff if (alloc more memory) { ... status = OK; } else status = NO_MEM } else status = NO_FILE } else status = NO_MEM } else status = NO_MEM return status; Or I get code that looks like: if (!allocate-memory) { status = NO_MEM; goto done; } do some stuff if (!get-more-memory) { status = NO_MEM; goto done; } do more stuff if (!openfile) { status = NO_FILE; goto done; } do even more stuff if (!alloc memory) { status = NO_MEM; goto done; } last stuff status = OK; done: return status; When what I really want is: if (!allocate-memory) return NO_MEM do some stuff if (!get-more-memory) return NO_MEM do more stuff if (!openfile) return NO_FILE do even more stuff if (!alloc memory) return NO_MEM last stuff return OK; A quick check reveals that the above code segments are all the same. However, I may have missed something. The final one is the clearest one, IHO, of them all. Comments.... -- Warner Losh imp@Solbourne.COM How does someone declare moral bankruptcy?