[Twisted-Python] My pet peeve ugly coding style

Glyph Lefkowitz glyph at twistedmatrix.com
Thu Jul 10 15:08:02 MDT 2003


On Thursday, July 10, 2003, at 02:17 PM, Tommi Virtanen wrote:

>> It is the very reason .addCallback returns self.
>
> 	Yes, and that's part of the problem; not the reason
> 	why ugly code should be tolerated.

This is a highly subjective discussion, of course.  IMHO, the 
multiple-lines-of-chained-addCallback style is easier to both to read 
and write because, provided you use a sensible editor, the indentation 
will end up something like this instead:

   return foo (
     ).addCallback(bar
     ).addCallback(baz
     ) # ...

If the proper placement of parentheses is a particular problem for you, 
consider

   return (foo()
           .addCallback(bar)
           .addCallback(baz)
           .addCallback(boz))

which is a style that I often prefer.

If you think _that_ is too ugly, then I wonder how you would wrap 
parameters across multiple lines - however, this ugliness will only 
_really_ be solved by adding some kind of block syntax to python.  
Personally I have better things to do at the moment.

As in any coding style discussion, consistency wins over correctness, 
so for Twisted, this is the "right way" to chain callbacks as far as 
I'm concerned :).





More information about the Twisted-Python mailing list