Defending the ternary operator

Andrew Koenig ark at research.att.com
Sat Feb 8 18:16:33 CET 2003


>> The essence of Laura's argument, I think, is that many programmers
>> will not become familiary with if-expressions, as they're used only
>> from time to time, which means that when they do encounter them,
>> it will be a mental stumbling block.

Alexander> I think you're missing one important aspect:
Alexander> predicatability of the sort that syntatic uniformity buys
Alexander> you. That is because it allows you to parse larger chunks,
Alexander> e.g. you always know that when you run into the token "if"
Alexander> the context is as follows: ``if CONDITION: DO_SOMETHING
Alexander> ...``.

This claim is already not true:

        y = [i for i in x if i != 0]

Alexander> Introducing an "if" expression essentially means that both
Alexander> forms will be used interchangably for a certain percentage
Alexander> of cases, which I'd guess, makes *all* if expressions more
Alexander> difficult to read (at least once interchangable use of both
Alexander> forms has reached a certain frequency -- if that ever
Alexander> happens (as I think it has in Ruby and Perl, BTW)).

I doubt it, because statements aren't interchangeable with expressions
in general.  If they were, there would be no need for a conditional
expression because we would already have one.

Try some examples and you'll see what I mean.


-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list