On Sun, Dec 08, 2002 at 03:12:29PM +0900, Pete McBreen wrote:
> Modern OO languages by comparison, only have 1 way of entering a  
> routine, so early exit does not have anywhere near the problems that  
> assembler programmers were able to cause. Indeed, it is common to have  
> multiple exits. For example, Martin Fowler's book Refactoring talks  
> about the use of Guard clauses which explicitly encourages multiple  
> returns (exits).
> 
> http://www.refactoring.com/catalog/ 
> replaceNestedConditionalWithGuardClauses.html
> 
> So my take on this is that while some of the old structured programming  
> rules are still useful, we need to reevaluate all in the light of  
> modern programming paradigms.

I agree with you that multiple exits should not be eliminated as a
rule.  Often, my code uses multiple exits. However, I would note that
the refatoring link is using a language where the form of the return
data is static.

Because the return type is not fixed in Ruby, we need to make a little
more judicious use of multiple exits. At the very least, you should
think carefully about returning objects that are different along
different exit paths. For instance, I would try to avoid code like
this:

def multiexit(choice)
  case choice
  when :1
    return [1,2,3]
  when :2
    return File.new('/tmp/blah')
  when :3
    return nil
  end
end

My example returns wildly different objects, but subtly different
objects might be even harder to debug given multiple exits.

-alan

-- 
Alan Chen
Digikata Computing
http://digikata.com