On Fri, 4 Oct 2002, Bob Miller wrote:

> Larry Price wrote:
> 
> > : return statements that aren't on either the top or the innermost level
> > of a function are a sign of trouble.
> 
> Now that's a weird one.  So this code, e.g., is a sign of trouble?
> 
>     class StatSample:
>         # ...
>         def variance(self):
>           if len(self) == 0:
>               return 0
>           sum, sum2 = 0, 0
>           for d in self:
>               sum += d
>               sum2 += d * d
>           return (sum2 - (sum * sum) / len(self)) / (len(self) - 1)

not at all, since (counting the method as a function) the first return is
at the deepest (innermost level) and the second is at the topmost (outer)
for that function. 

Bad would be:
def cracked(bar):
   if 0 < bar < 10:
        while bar:
                do_stuff()
                bar -=1
        return

Very contrived example I know, it's hard to make myself write something
that would violate the heuristic I mentioned.



-- 
http://www.efn.org/~laprice        ( Community, Cooperation, Consensus
http://www.opn.org                 ( Openness to serendipity, make mistakes
http://www.efn.org/~laprice/poems  ( but learn from them.(carpe fructus ludi)

_______________________________________________
Eug-LUG mailing list
[EMAIL PROTECTED]
http://mailman.efn.org/cgi-bin/listinfo/eug-lug

Reply via email to