[comp.lang.c] Multiple Returns

DHowell.ElSegundo@Xerox.COM (08/04/87)

There have been several messages posted concerning code with multiple
returns.  I am currently going through a two page routine with a return
on almost every other line, and let me tell you, it is nothing but a
mess.  I think that if you need to put a return in the middle of a
routine, you haven't really thought out the organization of your
algorithm.  Whatever happened to the structured programming concept of
one entry and one exit?  With multiple returns, in order to figure out
the conditions on a certain piece of code, I have to follow the code
through, looking at every conditional return to determine whether it
applies or not.  I would much rather see the code in nested if
statements, where I can immediately see which conditions apply to the
piece of code I am concerned with.

Dan <DHowell.ElSegundo@Xerox.COM>

edw@ius1.cs.cmu.edu (Eddie Wyatt) (08/05/87)

In article <8667@brl-adm.ARPA>, DHowell.ElSegundo@Xerox.COM writes:
> 
> There have been several messages posted concerning code with multiple
> returns.  I am currently going through a two page routine with a return
> on almost every other line, and let me tell you, it is nothing but a
> mess.  I think that if you need to put a return in the middle of a
> routine, you haven't really thought out the organization of your
> algorithm.  Whatever happened to the structured programming concept of


    X find_x(list,object)
	Y list;
	OBJ object;
	{
        X j;
        Y i;

	for (i = list->first; i != NULL; i = i->next)
	    for (j = i->first; j != NULL; j = j->next)
		if (j->object == object) return(j);

	return(NULL);
	}

   Returns in the middle of a routine are a good way to break from multi-level
loops. No design flaw here.

> one entry and one exit?  With multiple returns, in order to figure out
> the conditions on a certain piece of code, I have to follow the code
> through, looking at every conditional return to determine whether it
> applies or not.  I would much rather see the code in nested if
> statements, where I can immediately see which conditions apply to the
> piece of code I am concerned with.
> 
> Dan <DHowell.ElSegundo@Xerox.COM>

Consider an alternative - is this any better? worse?

    X find_x(list,object)
	Y list;
	OBJ object;
	{
        X j;
        Y i;
      	int found = FALSE;

	for (i = list->first; i != NULL && !found; i = i->next)
	    for (j = i->first; j != NULL && !found; j = j->next)
		found = (j->object == object);

	return(j);
	}
-- 

					Eddie Wyatt

e-mail: edw@iinge
> @be, and

henry@utzoo.UUCP (Henry Spencer) (08/06/87)

> There have been several messages posted concerning code with multiple
> returns.  I am currently going through a two page routine with a return
> on almost every other line, and let me tell you, it is nothing but a
> mess...

It sounds to me like you have diagnosed the problem correctly:  the routine
is a mess, and probably would be just as much of a mess if the "programmer"
who wrote it had used a single return.  Nested ifs five levels deep written
by an incompetent are no easier to understand than multiple returns written
by an incompetent, I assure you.  A competent programmer makes either one
understandable, although my own tentative opinion is it's a bit easier with
multiple returns.
-- 
Support sustained spaceflight: fight |  Henry Spencer @ U of Toronto Zoology
the soi-disant "Planetary Society"!  | {allegra,ihnp4,decvax,utai}!utzoo!henry