Tertiary Operation
Roy Smith
roy at panix.com
Tue Oct 17 10:10:19 EDT 2006
In article <1161093117.541719.320210 at i3g2000cwc.googlegroups.com>,
"abcd" <codecraig at gmail.com> wrote:
> Carsten Haese wrote:
> > Use Python 2.5 where there is a true conditional
> > expression or find another way to solve your problem.
>
> python 2.5 once we upgrade (hopefully soon), anyways...an earlier post
> suggested the inverse...
>
> x = None
> result = (x is not None and str(x) or "")
>
> which works just fine.
>
> thanks.
Why not just:
if x is None:
result = str(x)
else:
result = ""
It's a couple more lines of code, but it's obvious what it means. I know
about boolean short-circuit evaluation, and the and/or trick, but I still
look at
result = (x is not None and str(x) or "")
and have to puzzle through exactly what's going on there. You are going to
write your code once. It's going to be read many many times by different
people. It's worth a few more keystrokes on your part to save all those
future maintenance programmers headaches later.
You won't really understand just how important readability is until you're
maintaining a million lines of old code, most of it written by people who
are no longer on the project. I spend a lot of time staring at code
somebody else wrote and muttering things like, "What the **** does this
do?" That's just money down the toilet.
If I have to reach for a reference manual to look up operator binding rules
to understand a piece of code, that's bad. If I have to start making notes
on a piece of scrap paper, "Let's see, if x is this, then that's true, so
blah, blah", that's bad. If I look at something subtle, don't realize it's
subtle, and come to an incorrect conclusion about what it does, then base
some other decisions on that incorrect conclusion, that's really, really
bad.
More information about the Python-list
mailing list