TITLE: A Tax Form is Really a Program AUTHOR: Eugene Wallingford DATE: April 11, 2016 2:53 PM DESC: ----- BODY: I finally got around to preparing my federal tax return this weekend. As I wrote a decade ago, I'm one of those dinosaurs who still does taxes by hand, using pencil and paper. Most of this works involves gathering data from various sources and entering numbers on a two-page Form 1040. My family's finances are relatively simple, I'm reasonably well organized, and I still enjoy the annual ritual of filling out the forms. For supporting forms such as Schedules A and B, which enumerate itemized deductions and interest and dividend income, I reach into my books. My current accounting system consists of a small set of Python programs that I've been developing over the last few years. I keep all data in plain text files. These files are amenable to grep and simple Python programs, which I use to create lists and tally numbers to enter into forms. I actually enjoy the process and, unlike some people, enjoy reflecting once each year about how I support "we, the people" in carrying out our business. I also reflect on the Rube Goldberg device that is US federal tax code. However, every year there is one task that annoys me: computing the actual tax I owe. I don't mind paying the tax, or the amount I owe. But I always forget how annoying the Qualified Dividends and Capital Gain Tax Worksheet is. In case you've never seen it, or your mind has erased its pain from your memory in an act of self-defense, here it is:
Qualified Dividends and Capital Gain Tax Worksheet--Line 44
It may not seem so bad at this moment, but look at that logic. It's a long sequence of "Enter the smaller of line X or line Y" and "Add lines Z and W" instructions, interrupted by an occasional reference to an entry on another form or a case statement to select a constant based on your filing status. By the time I get to this logic puzzle each year, I am starting to tire and just want to be done. So I plow through this mess by hand, and I start making mistakes. This year I made a mistake in the middle of the form, comparing the wrong numbers when instructed to choose the smaller. I realized my mistake when I got to a line where the error resulted in a number that made no sense. (Fortunately, I was still alert enough to notice that much!) I started to go back and refigure from the line with the error, when suddenly sanity kicked it. This worksheet is a program written in English, being executed by a tired, error-prone computer: me. I don't have to put up with this; I'm a programmer. So I turned the worksheet into a Python program. This is what the Qualified Dividends and Capital Gain Tax Worksheet for Line 44 of Form 1040 (Page 44 of the 2015 instruction book) could be, if we weren't still distributing everything as dead PDF:
line   = [None] * 28

line[ 0] =      0.00                     # unused
line[ 1] =      XXXX                     # 1040 line 43
line[ 2] =      XXXX                     # 1040 line  9b
line[ 3] =      XXXX                     # 1040 line 13
line[ 4] = line[ 2] + line[ 3]
line[ 5] =      XXXX                     # 4952 line  4g
line[ 6] = line[ 4] - line[ 5]
line[ 7] = line[ 1] - line[ 6]
line[ 8] =      XXXX                     # from worksheet
line[ 9] = min(line[ 1],line[ 8])
line[10] = min(line[ 7],line[ 9])
line[11] = line[9] - line[10]
line[12] = min(line[ 1],line[ 6])
line[13] = line[11]
line[14] = line[12] - line[13]
line[15] =      XXXX                     # from worksheet
line[16] = min(line[ 1],line[15])
line[17] = line[ 7] + line[11]
line[18] = line[16] - line[17]
line[19] = min(line[14],line[18])
line[20] = 0.15 * line[19]
line[21] = line[11] + line[19]
line[22] = line[12] - line[21]
line[23] = 0.20 * line[22]
line[24] =      XXXX                     # from tax table
line[25] = line[20] + line[23] + line[24]
line[26] =      XXXX                     # from tax table
line[27] = min(line[25],line[26])

i = 0
for l in line:
    print('{:>2} {:10.2f}'.format(i, l))
    i += 1
This is a quick-and-dirty first cut, just good enough for what I needed this weekend. It requires some user input, as I have to manually enter values from other forms, from the case statements, and from the tax table. Several of these steps could be automated, with only a bit more effort or a couple of input statements. It's also not technically correct, because my smaller-of tests don't guard for a minimum of 0. Maybe I'll add those checks soon, or next year if I need them. Wouldn't it be nice, though, if our tax code were written as computer code, or if we could at least download worksheets and various forms as simple programs? I know I can buy commercial software to do this, but I shouldn't have to. There is a bigger idea at play here, and a principle. Computers enable so much more than sharing PDF documents and images. They can change how we write many ideas down, and how we think. Most days, we barely scratch the surface of what is possible. -----