Coding Standards. was: a style question

John Stanley stanley at phoenix.com
Sat Nov 17 17:07:05 AEST 1990


imp at marvin.Solbourne.COM (Warner Losh) writes:

>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".  

  >>lots of code deleted<<

>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....

   Ooh, an invitation for comments!

   The main advantage to having one exit point is that it is easier to be
sure any necessary cleanup is performed prior to exit. It can be quite
messy when cleanup code is distributed in multiple copies throughout the
file. The example that I deleted involved opening files, then doing
something, then exiting if that something caused an error. This type of
situation makes it very easy to forget to close that file. If you call
this function many times, and have many errors, you will eventually run
out of files.

   It was just this bug I found in some production code, a year after it
was released. I put the software into a neverending demo loop during a
trade show and noticed that it started complaining about "no more units"
after 20 minutes or so. The code was bullet proof enough that it didn't
crash, just could no longer access any data. If the code had had but one
exit from that routine, it would probably have never had that bug get by.




<> "Aneth!  That's a charming place!" "You've been to Aneth?" 
<> "Yes, but not yet." -- The Doctor and Seth, "The Horns of Nimon".
><
<> "Sanity check!" "Sorry, we can't accept it, it's from out of state." - me



More information about the Comp.lang.c mailing list