Defending the ternary operator

Alexander Schmolck a.schmolck at gmx.net
Sat Feb 8 11:50:06 EST 2003


Andrew Koenig <ark at research.att.com> writes:
> As I noted (with variations) in another thread, I think that
> readibility is proportional to brevity * familiarity.

Almost, but not quite (see below).

> So something short but unfamiliar is hard to read until you
> become familiar with it.
> 
> 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.

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

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

That is not to say that your point about familiarity (and thus the trade-off
between catering to new or infrequent users and power users) isn't
valid. Someone who already mastered all the "old" features has much more to
gain from the introduction of partly overlapping new ones than someone who
hasn't -- which is one of the reasons why languages start out small and bloat.

Indeed, although my feeling is that introducing a "ternary operator" to python
would do a disservice to the language, I'd be more than happy to use it
myself.


alex




More information about the Python-list mailing list