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

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Jul 12 10:40:43 EDT 2002


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.

Eddie



More information about the Python-list mailing list