Hi --

On Mon, 19 Feb 2007, David Vallner wrote:

> On Mon, 19 Feb 2007 14:43:07 +0100, <dblack / wobblini.net> wrote:
>
>> Hi --
>> 
>> No; Chris knows what he's doing.  It's pretty fundamental -- I always
>> make a point of explaining it when I introduce the topic of methods to
>> beginners.  Otherwise simple things like:
>>
>>   def of_age?
>>     age > 17
>>   end
>> 
>> make no sense.  It's basically just a function that performs a
>> calculation.
>> 
>
> For mathemathical functions with a simple enough control flow, I'm ambivalent 
> on the topic. However once a method has a premature return (or four) 
> somewhere, the inconsistency hurts my sensitivities. Using the keyword in 
> such a method as a matter of style gives me one less thing to keep in my head 
> when scanning the code for exit points - either there is only one and it's 
> completely obvious like in your example, or it's every line with "return" in 
> it.

But if an exit point is midway through a method, it will have to have
a return:

   def m(x)
     return false unless x > 0
     blah
     blah
     return x if x == 1
     blah
     blah
   end

I can't drop the "return"s on those, because the values will just be
thrown away and the method will continue.

The only time you'd see this not literally at the very end of the
method, I think, is if you've got something like an if/else or case
statement:

   def m(x)
     if x > 0
       false
     else
       ...
     end
   end

I agree that if too much happens afterwards -- if you stick a whole
bunhc of lines in that else -- it can be hard to catch the fact that
"false" is a potential exit point.  I'd rather see a hard-coded
conditional return -- sort of in the spirit of "break" -- and then get
on with the method.


David

-- 
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
    (See what readers are saying!  http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)