2009/4/20 david <youngde...@gmail.com>:
> When I was at Data General, writing C (and a little C++), we had a set
> of internal coding conventions that mandated a single return point for
> a function.

How long ago was that? Or, more relevant, how old was the rule? Or how
long earlier had the person who wrote the rule learned their craft?
One reason for mandating single exit points was that early flow graph
reducers couldn't handle them, but, because of the GOTO equivalents
that you give, graphs with multiple exit points /are/ reducible
without the exponential blowup that node splitting can cause, so
they've been able to handle multiple exits for many years.

Of course, your code is equivalent to:

int
frodo() {
 int rval = 0;

 if (bilbo() != 0) rval = -1
 else
 {
   if (gandalf() != 0) rval = -1
   else
   {
     /* lots of code here */
   }
 }
 return rval;
}

with not a GOTO in sight, and to my mind much clearer logic. If the
nesting meant that the indentation was marching off the side of the
page I'd refactor the "lots of code here" into an inline helper
function. And provided bilbo() and gandalf() don't have side effects,
I'd probably rewrite it as:

int
frodo()
{
 int rval = 0;

 if ((bilbo() == 0) || (gandalf() == 0)
 {
   /* lot's of code here */
 }
 else rval = -1;
 return rval;
}

I'd be inclined to do it that way even if multiple exits were allowed;
it just seems so much clearer.

-- 
Tim Rowe
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to