Defending the ternary operator

Jp Calderone exarkun at intarweb.us
Sat Feb 8 17:31:09 EST 2003


On Sat, Feb 08, 2003 at 04:50:06PM +0000, Alexander Schmolck wrote:
> 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 ...``. 

  To expand on this, a bit...

  Let's refer back to the PEP itself:

        x if C else y if D else z <==> x if C else (y if D else z)
        x or y if C else z        <==> (x or y) if C else z
        x if C else y or z        <==> x if C else (y or z)
        lambda: x if C else y     <==> lambda: (x if C else y)
        x if C else lambda: y     <==> SyntaxError
        x if C else y, z          <==> (x if C else y), z
        x, y if C else z          <==> x, (y if C else z)

  Now, to me, at least 3 of the above forms will be confusing, until I
memorize the new precedence relationships this introduces.  Any time I read
one of these unfamiliar groupings, I'm going to have to stop and find the
documentation that describes this.  There is no predicability here - the
precedence has been arbitrary decided.  In isolation this form may be
perfectly readable, but does anyone really expect that isolation is going to
be the most common use?

  Jp

-- 
It is practically impossible to teach good programming style to
students that have had prior exposure to BASIC: as potential
programmers they are mentally mutilated beyond hope of
regeneration.        -- Dijkstra
-- 
 up 2:28, 5 users, load average: 0.39, 0.30, 0.23
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030208/f7e4598c/attachment.sig>


More information about the Python-list mailing list