Defending the ternary operator

Max M maxm at mxm.dk
Mon Feb 10 06:08:51 EST 2003


Andrew Dalke wrote:
> Paul Paterson
> 
>>If my regular expression is correct (and it probably isn't because I'm not
>>good at them), then a quick scan of my Python22 directory structure finds
>>769 matches of the following pattern,
>>
>>    if <something>
>>        variable = <some value>
>>    else:
>>        variable = <some other value>
> 
> 
> Oh yeah, and you should also check for
> 
>   if <something>:
>     return <some value>
>   return <some other value>


Another thing to take into account, is the evolution af a programme. 
Often you start up with just one condition:

if <something>
     variable = <some value>
else:
     variable = <some other value>

You might write this with the terniary op:

<something> ? <some value> : <some other value>

But very often, a few lines later, you will need to at another 
condition, or add some temperary calculations.

if <something>:
     <new value> = 21*2
     variable = <some value>+<new value>
elif: <something else>
     variable = <some third value>
else:
     variable = <some other value>

Then you will need to rewrite the terniary operator. If you had written 
it with an if/else to begin with you wouldn't need to rewrite as much.

In my experience it would hapen so often that using the terniary would 
probably require more coding than not using it.

-- 

hilsen/regards Max M Rasmussen, Denmark

http://www.futureport.dk/
Fremtiden, videnskab, skeptiscisme og transhumanisme





More information about the Python-list mailing list