Hi,

I'm sure this has been discussed before and we all have our own system, but, I am wondering. How do you check status (and stop execution if an error occured) while maintaining the concepts of single exit point and keeping the code clean.
After trying several systems, the one I feel most comfortable with is something like:

Code:
Status f() {
 Status ret = SUCCESS;

 ret = g1();
 if (ret != SUCCESS)
   goto bail;

 ret = g2();
 if (ret != SUCCESS)
   goto bail;
...
...
bail:
  if (ret != SUCCESS) {
    cleanup....
  }
  return ret;

}
There are pros and cons for every system (the con here is the use of goto, the pro - one cleanup code only and single exit point)

Other possible solutions:

- very very nested ifs (This is called an arrow code because of its shape)
Code:
ret = g1();
if (ret) {
  ret = g2();
  if (ret) {
  ...
  }
}
- put a return in every failure - many exit points (maybe not that bad?)
Code:
if (!g1())
  return;

if (!g2())
  return;
- use a loop to surround the function and break when error occurs

Code:
while(1) {
  if ( !(ret = g1()) )
    break;
  if ( !(ret = g2()) )
    break;  
}
- theres probably alot more.

If there are articles on this issue that i am not aware of I'd be glad to recieve a link.
I'd like to hear suggestions or thoughts about this issue.

Thank you!