Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!rutgers!rochester!pt!ius1.cs.cmu.edu!edw From: edw@ius1.cs.cmu.edu (Eddie Wyatt) Newsgroups: comp.lang.c Subject: Re: Multiple Returns Message-ID: <1026@ius1.cs.cmu.edu> Date: Tue, 4-Aug-87 22:28:16 EDT Article-I.D.: ius1.1026 Posted: Tue Aug 4 22:28:16 1987 Date-Received: Fri, 7-Aug-87 05:02:36 EDT References: <8667@brl-adm.ARPA> Organization: Carnegie-Mellon University, CS/RI Lines: 57 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 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