reduce()--what is it good for? (was: Re: reduce() anomaly?)

Steve Lamb grey at despair.dmiyu.org
Tue Nov 18 12:04:34 EST 2003


On 2003-11-18, Andrew Dalke <adalke at mindspring.com> wrote:
> While I, when writing Perl code, also NEVER had a problem.  I
> ignored just about all of its variations
>   if ($cond) {$a++}
>   $a++ if $cond;
>   $a++ unless !($cond);
>   unless (!($cond)) {$a++};

    Exactly the case I was thinking of when I wrote my message.  4 ways to
express an if.  After getting over the neatness factor I wrote if in exactly
one way.  if ($cond) { block }.  I found that by doing so my code was not only
far more readable to other people in general it was readable by *ME* just a
few weeks later.

    I think the same thing every time we get someone asking for do...until or
do...while.  We have while.  If I want exactly 1 run before the condition is
tested I can do that.  It seems far clearer to me when I see...

x = 1
while x < 2:
   x = foo()
print "done"

   ...what is going on than having to deal with someone putting:

do:
    x = foo()
while x < 2:
print "done"

    ...or having to deal with people who will read my code and tell me "Ya
know, you should use a do...while here."  "Why?  It's just another form of
while."  "Well, yeah, but it guarentees the loop performs once."  "So?
Initilizing your variables does that, too."

> I did this not because it was the most expressive but because I wanted to
> write code that others could follow, and indeed one of the praises I got was
> "wow! It's Perl code I can actually understand!"

    Same here.  The latest compliment was that upon seeing an example of
Python and PHP code that did the same thing that my code was "concise and
clear".  It is because I code with consistent structure, not with expressive
structure.

-- 
         Steve C. Lamb         | I'm your priest, I'm your shrink, I'm your
       PGP Key: 8B6E99C5       | main connection to the switchboard of souls.
-------------------------------+---------------------------------------------




More information about the Python-list mailing list