For review: PEP 308 - If-then-else expression

Paul Paterson hamonlypaulpaterson at houston.rr.com
Sat Feb 8 22:30:16 EST 2003


"Andrew Dalke" <adalke at mindspring.com> wrote in message
news:b24dr3$bv5$1 at slb0.atl.mindspring.net...
> Carlos Ribeiro:
> > Thanks again for pointing out the type of situation where I expect
> conditional
> > expressions to be most useful. I find it amazing that many people don't
> > regard long blocks of code as a problem; as for myself, I get lost with
> > blocks that don't fit on the screen.
>
> My analysis suggests that this will, on average, make code
> about 0.04% smaller.  This was based on analyzing the number of
> time ?: is used in a large C++ codebase which was written by
> programmers of moderate experience and counting the times it was
> used appropriately (for C++), appropriately (if it was Python), and
> inappropriately (for either language).  It was used inappropriately
> more than it was used appropriately.
>
> Andrew Koenig suggests a higher use rate, but that was based on C code
> (so no objects, which mitigate a few of the problems) written by an
> expert programmer.  His rate was 1/400 lines, of which I think a few
> were debateable and a few more unneeded in Python.  Apply a scaling
> factor and assume it's used once per 200 lines of Python code, and
> that each use saves 3 lines.  Then the code is, on average, 1.5%
> smaller.
>

As per a different thread where I did the reverse and searched the Python
code in the Python standard library + assorted packages on my system. I
found,

769 candidates where the conditional expression could be used to replace an
assignment
381210 lines of code

So as a rough estimate we get one use per (381210 - 4*769)/769 = 491 /lines
of code.

The actual usage is probably a lot more common that this as I only looked
for the assignment pattern, but I would say the 1 in 200 lines is a fairly
reasonable estimate.






More information about the Python-list mailing list