PEP 308: A PEP Writer's Experience - PRO
Anders J. Munch
andersjm at dancontrol.dk
Thu Feb 13 06:05:07 EST 2003
"Andrew Dalke" <adalke at mindspring.com> wrote:
> rate if/else could be used == 1 every 400 LOC
> rate it would be used == 1 every 1,000 LOC
> rate where short circuiting is needed == 1 every 5,000 LOC
> rate if/else expression used in an if statment == 1 every 10,000 LOC
> rate where "something and this or that"
> would not suffice (larger uncertainty here) == 1 every 25,000 LOC
> rate used as "if (if something: this else: that)" == 1 every 40,000 LOC
>
> It's remarkably easy to do this sort of analysis using grep and
> a reasonably large C/C++ codebase. Several appropriate codebases
> are available to you.
To every hard problem, there's a solution that is simple, obvious, and
wrong. (Sorry, couldn't resist.)
Your searches are all single-line. Conditional expressions broken
over multiple lines are not uncommon.
In my personal use, conditional expressions are used a lot in
debugging diagnostics, which are later deleted. The frequency of
conditionals in any codebase snapshot may not reflect the frequency of
using conditionals.
>
> So again I ask you, what rate of misuse is low enough as to
> not be a problem? 1%? 10%? 50%? 0.01%? Apparently
> it's above 3%.
No examples I have seen so far qualify for the strong term "misuse".
Bad style perhaps, though I'm not even sure that
> if (something ? this : that) ...
is bad style. Can you suggest an alternative? The only semantically
neutral rewrite I can think of is
bool something_var = something;
if((something_var && this) || (!something_var && that))
which I'd hesitate to call an improvement.
>[...]and I include
> cases where the if/else expression will be used in lieu of better/
> more appropriate cases, as in "x = (if obj: obj else: default)"
> instead of "x = obj or default".
"x = (if obj: obj else: default)"
is fine. In all likelyhood whoever wrote this is unfamiliar or
uncomfortable with "obj or default", and would not have used that
anyway. Also, it lends itself to a clarification such as
"x = (if obj is None: obj else: default)"
- Anders
More information about the Python-list
mailing list