break, continue, return, goto

Oleg Kiselev oleg at birtch.UUCP
Wed Nov 20 06:50:07 AEST 1985


In article <283 at 3comvax.UUCP> mikes at 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



More information about the Comp.lang.c mailing list