Re: [Hampshire] Code style

Top Page
Author: Hugo Mills
Date:  
To: Hampshire LUG Discussion List
Subject: Re: [Hampshire] Code style

Reply to this message
gpg: failed to create temporary file '/var/lib/lurker/.#lk0x58183100.hantslug.org.uk.29269': Permission denied
gpg: keyblock resource '/var/lib/lurker/pubring.gpg': Permission denied
gpg: Signature made Mon Jun 8 14:57:01 2009 BST
gpg: using DSA key 20ACB3BE515C238D
gpg: Can't check signature: No public key
On Mon, Jun 08, 2009 at 02:53:12PM +0100, Daniel Pope wrote:
> Hugo Mills wrote:
> >     I happily thought that the ternary operator had been put to death
> > in Python, until I saw this little gem in some code yesterday:

> >
> >     (is_forward and "F" or "B")

> >
> > The perpetrator has been given a good talking-to. :)
>
> That's become a common idiom in Python, and most Pythonistas will have
> seen it. It falls over if where you've written "F" there's an expression
> that will evaluate to false.


Ugly *and* fragile. <sarcasm>Spiffing.</sarcasm>

> It was replaced by a proper ternary operator in Python 2.5. But it's not
> a C-style ternary operator, it's
>
> result = 1 if test() else -1
>
> the idea being that the difference in syntax stresses the success path
> as the default with the failure path as a fallback.


Eww. That's *intensely* ugly.

Yet one more reason to avoid ternary operators... (If you haven't
guessed yet, I'm not a fan of the whole concept. I've rarely met a use
of the ternary operator, in any language, that made code easier to
read.)

Hugo.

-- 
=== Hugo Mills: hugo@... carfax.org.uk | darksatanic.net | lug.org.uk ===
  PGP key: 515C238D from wwwkeys.eu.pgp.net or http://www.carfax.org.uk
  --- I hate housework. You make the beds, you wash the dishes, and ---  
           six months later you have to start all over again.