| Store | Cart

What does "Sparse is better than dense" mean? (Python Zen)

From: Eddie Corns <edd...@holyrood.ed.ac.uk>
Fri, 12 Jul 2002 14:40:43 +0000 (UTC)
Just adding my tuppence worth.  Remember that whitespace can be added
horizontally too eg.

for line in xreadlines.xreadlines(sys.stdin):
    if   line[0]  == '#'     : continue
    elif line[:5] == 'DATE:' : yield ('DATE', scan_date (line))
    elif line[:5] == 'INTF:' : yield ('INTF', scan_intf (line))
    elif line[:4] == 'ETH:'  : continue # for now
    elif line[:4] == '    '  : continue
    else                     : yield ('DATA', scan_data (line))
Basically, when you layout code you need to think about how to convey to the
reader what's going on.  In constructions like the above I believe it's much
easier to see that it's only ever looking at the beginning of the line and the
table like structure makes it easy to map what it finds to what gets done.
(this code was extracted from a quick one-off so I'm not interested in
comments about how good it is!).

I used align.el, which I discovered recently to do the work, it does blocks of
assignments very easily, other things take a little work.

I also occasionally collapse single if statements if I feel they are less
important to the flow.


Recent Messages in this Thread
Alex Martelli Jul 11, 2002 02:28 pm
Martijn Faassen Jul 11, 2002 04:54 pm
Alex Martelli Jul 11, 2002 08:28 pm
Donn Cave Jul 11, 2002 09:17 pm
James J. Besemer Jul 12, 2002 05:43 am
Eddie Corns Jul 12, 2002 02:40 pm
Donn Cave Jul 12, 2002 05:18 pm
James J. Besemer Jul 13, 2002 06:01 am
Donn Cave Jul 13, 2002 07:26 am
James J. Besemer Jul 13, 2002 10:34 am
Michael Hudson Jul 15, 2002 02:47 pm
Aahz Jul 13, 2002 03:28 pm
Alex Martelli Jul 12, 2002 07:33 am
James J. Besemer Jul 12, 2002 08:33 am
Messages in this thread