Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site birtch.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!tektronix!hplabs!felix!birtch!oleg From: oleg@birtch.UUCP (Oleg Kiselev) Newsgroups: net.lang.c Subject: Re: break, continue, return, goto Message-ID: <132@birtch.UUCP> Date: Tue, 19-Nov-85 15:50:07 EST Article-I.D.: birtch.132 Posted: Tue Nov 19 15:50:07 1985 Date-Received: Sat, 7-Dec-85 21:07:39 EST References: <771@whuxl.UUCP> <9500029@iuvax.UUCP> <806@whuxl.UUCP> <283@3comvax.UUCP> Reply-To: oleg@birtch.UUCP (Oleg Kiselev) Organization: The Real Estate Conspiracy Lines: 62 Summary: In article <283@3comvax.UUCP> mikes@3comvax.UUCP (Mike Shannon) writes: >Michael Baldwin: >> Can anyone suggest a recoding of the following example without using >> multiple returns? Of course it can be done, but in a clearer, simpler ^^^^^^^^^ ^^^^^^^^^^^^^^^^!!! >> way? Remember, this is exactly the same as using loops with continue. >> >> core(file) >> char *file; >> { >>[CODE WITH MULTIPLE RETURNS] >> } >-------- >OK, here goes: >-------- >core(file) >char *file; >{ >[SIMILAR CODE USING IF...THEN...ELSE IF....] >} > >----- > This case is *exactly* the situation where I don't like to see >multiple returns. If there's one thing I hate, it's seeing a bunch of >branch statements. Then I have to scratch my head and think "Ok, where >can I get to as I scan down this code?". Really. "return" is not much of a "branch" statement. It is fairly unambiguous. What it say to ME is "this is it! No need to look farther. Return to caller" > I really think my second way is better. If I had to debug this >code, I would read it and it's immediately obvious what's going on. This >'multiple error filter' use of the if.... else if..... else seems >so intiutive to me that I can trivially scan it and instantly know >what's going on. Without multiple returns you have to scan the entire routine and check every "if" and "switch", make sure there is no problems with improperly balanced "if...then...else if"s, etc. "return" just tells you very plainly : "If this is the case, take action, and RETURN". That's it! no need do follow a lengthy and convoluted ( and possibly buggy) chain of "if"s, no need to scatter your attention on the entire page of code, trying to find out what actions OTHER than the ones specified in the original "if" are taken: what flags are set, or reset; what other routines might be called, etc. With "return" there is no such problem - you LEAVE the routine! > Michael Shannon {ihnp4,hplabs}!oliveb!3comvax!mikes Your example might be scholasticaly more proper (no goto's, multiple returns, breaks, continues) but it is NOT *clearer* or *simpler*! In fact, why do you think so many NON-standard Pascal compilers have "LEAVE" instruction to break out of a loop or return early from a procedure? Because Pascal does not allow multiple "return"s and people seem to need them badly enough to put them into the language! -- Disclamer: My employers go to church every Sunday, listen to Country music, and donate money to GOP. I am just a deviant. +-------------------------------+ Don't bother, I'll find the door! | "VIOLATORS WILL BE TOAD!" | Oleg Kiselev. | Dungeon Police |...!{trwrb|scgvaxd}!felix!birtch!oleg --------------------------------+...!{ihnp4|randvax}!ucla-cs!uclapic!oac6!oleg