Kurt McKee

lessons learned in production


Hey there! This article was written in 2009.

It might not have aged well for any number of reasons, so keep that in mind when reading (or clicking outgoing links!).

Replacing magical code

Posted 20 September 2009 in listparser

listparser has had some magical code in it for a while, and today I removed the two most obvious magics.

If you've ever looked at the code and have seen the expect-related code, you've probably said "wat". Yes, the code was absurd, but it's now gone. If you don't know what I'm talking about, just appreciate that the stage has been set for support for another subscription list format.

The other big change (and the one I'm most pleased with) is the replacement of the tag and category code. When I first started writing it, list comprehensions seemed like an obvious solution. Unfortunately, as I discovered corner cases I continued to solve the problem with more list comprehensions. Eventually I ended up with this ugly mess:

def or_strip(x, y):
    return x.strip() or y.strip()
tags = [x.strip() for x in attrs[(None, 'category')].split(',') if x.strip() and '/' not in x]
cats = (x.strip() for x in attrs[(None, 'category')].split(',') if '/' in x)
cats = (x.split('/') for x in cats if reduce(or_strip, x.split('/')))
cats = (xlist for xlist in cats if reduce(or_strip, xlist))
cats = [[y.strip() for y in xlist if y.strip()] for xlist in cats]

Splits and strips and lists, oh my! All told, I replaced 15 lines of code with 16 lines but gained shorter line lengths and more maintainable code.

I'm looking forward to releasing the next version of listparser.