PEP 308: some candidate uses cases from live code

Andrew Dalke adalke at mindspring.com
Mon Feb 10 04:24:04 EST 2003


Martin Maney:
> Okay, a bleary-eyed survey of about 4000 lines of code from a project
> I've been working on in odd bits of time.  Best Available Data, sir,
> but mine own.  <wink>

Wow!  Thank you for your effort!

If I may summarize:

 Candidate 1  = maybe
 Candidate 2  = no
 Candidate 3  = yes
 Candidate 4  = maybe
 Candidate 5  = yes
 Candidate 6  = maybe

BTW, I completely agree with your estimates of which are
good vs. maybe vs. bad uses of the if/else expression.

At most there are 6 changes in 4,000 lines of code, which is about
1 every 667.  My estimate (based on an analysis of C/C++ code)
suggested numbers around 1,000 lines).  Someone else's estimate
(my apologies, don't recall who) was about 1 in 200 lines, but I found
some problems in that estimate.

Assume the maybes are 50%s.  That means there are about 3.5
proper uses of this in 40,000 LOC, or under 1 per 1,000 lines.
I interpret this to agree that it doesn't help much with overall
readability of Python code, since it won't be used all that often.

This assumes it will always be used when it is a better solution.
Not everyone will use the if/else expression.  Some because they
don't like it, others because it isn't part of their toolchest, etc.
I looked at some C/C++ code and found a reasonable number of
examples where if/else was used when ?: could have been used.
I did not do a good survey of this, so I'll estimate it at 75% usage,
so I can be on the high side, and round things out to 3 uses in
40,000 lines of code.


Now consider that people will use if/else expression to make
things more complex than otherwise.  There are 2.5 where it
could have been done, and I believe (as Bengt Richter was so
kind to point out ;) that some will do it.

(Earlier I listed some reasons: it's "tight" or "cool" or "terse",
or perhaps because "well, it's in the language so I'll use it" or
simply showing off.)  My observations of misuse in C++ code
showed it was pretty high, so I'll assume 50% and round down
to 1 use in 40,000 LOC.

That means that for every 3 uses of the if/else expression which
make the code more readable/usable/maintainable, etc. then
we should expect 1 use which make it less so.

The numbers I came up with were closer to 1 to 1, but that's
because 1) I was working with a codebase written by people
who are not software engineers, and 2) your analysis does not
include inappropriate cases where you could have used a
if/else expression but were already using a better/more appropriate
Python construct.

(eg, using "x if x < y else y" instead of "min(x, y)")

I'm willing to concur from what I've seen from those who have done
analysis based on real-life code that

  - if/else expression will be used well about once every 1,000 LOC

  - for every 2 proper uses of the if/else expression there will be
    1 improper use

Therefore, I am still against this PEP.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list