[Baypiggies] padding variable assignments, why is it bad?

Wai Yip Tung tungwaiyip at yahoo.com
Fri Aug 10 21:01:12 CEST 2012


Imagine your have some code like this

row.name = encode(src.get('name'), 'xmlref')
row.description = encode(src.get('description'), 'xmlref')
row.enabled = encode(src.get('enabled'), 'xmlref')
row.last_access = encode(src.get('last_access'), 'xmlref')

This looks a mumbo jumbo lines of code.

However by aligning them into columns, the pattern become immediately 
obvious.

row.name        = encode(src.get('name'),        'xmlref')
row.description = encode(src.get('description'), 'xmlref')
row.enabled     = encode(src.get('enabled'),     'xmlref')
row.last_access = encode(src.get('last_access'), 'xmlref')

It is clear that the code is applying similar operation on 4 items. It 
is a lot easier to tell which of the 4 elements and if you should 
include more or less into the list. It is a huge visualization 
improvement. PEP-8's suggestion on this is not a good one in my opinion.

The down side is it takes some effort to keep it neat and tidy. Of 
course I have editor macro that does that. I think every decent text 
editor should have some function to help people to align things.

Wai Yip




> Aleksandr Miroslav <mailto:alexmiroslav at gmail.com>
> Friday, August 10, 2012 10:19 AM
> (Reading this email requires a monospaced font.)
>
> I do this a lot in my code:
>
> foo = 1
> foo_bar = 2
> foo_bar_quux = 3
>
> but pep8, which I started using recently, considers this wrong, so I
> have to rewrite to the more common form:
>
> foo = 1
> foo_bar = 2
> foo_bar_quux = 3
>
> which I think sacrifices readability. Perhaps this is the n00b in me
> talking, but why is this considered bad?
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> http://mail.python.org/mailman/listinfo/baypiggies
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120810/2dfc81da/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compose-unknown-contact.jpg
Type: image/jpeg
Size: 770 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20120810/2dfc81da/attachment.jpg>


More information about the Baypiggies mailing list