The Pains of Programming
“To the programmer he said, ‘I will make you incompetent during the worst possible moments. It will take you hours to find and repair trivial errors.’ ”
-Programmer’s Bible: The book of Assembly Code, 3:16
Oh, how true it is.
For a few hours today and the better part of last night, I’ve been working on a program to calculate a line of best fit. It was a code exercise that took much longer than it had any right to.
It’s not that the code itself was difficult. I use Visual Studio for my C# code, so I got the window and controls set up fast. The line of best fit can also be calculated analytically. Just find the formula for the least-squares error, take the partial derivatives of A and B, and solve the system of equations. My code worked perfectly fine for solving the problem and spitting out A (the slope) and B (the y-intercept).
The problems came up when I tried graphing stuff. First, I found out that the computer counts pixels from the top left, while I want the origin at the bottom left. But when I reflected the drawing vertically about the y-axis (the transformation was NEW Y = HEIGHT OF WINDOW - OLD Y), all the points disappeared. I had the help of my dad, and we spent about two hours staring at the code and getting nowhere.
Here’s the breakdown of what I did with that chunk of code today:
5 minutes: Figuring out that HEIGHT OF WINDOW should have been HEIGHT OF THE PANEL WHERE THE GRAPH IS ACTUALLY DRAWN, WHICH IS LIKE HALF THE SIZE OF THE ENORMOUS WINDOW ITSELF. Now there’s your problem!
5 minutes: Suddenly realizing the old code only showed a fifth of the data points and that now I’ve got 100 more to deal with.
20 minutes: Wondering why the edge of the graph kept getting cut off and changing the size of the points on the graph to see why. Turns out that the data just happens to have a lot of points congregated to the right.
10 minutes: Wrestling with syntax and about a dozen sets of parentheses to draw a y-axis.
10 minutes: Figuring out a way to expand the boundaries of the data set without having to worry about signs.
10 minutes: Wondering why my attempt to draw the least-squares regression line doesn’t show up on the window.
2 minutes: Adding 0,0 to the data set to force it to be included in the graph, then seeing the outlier skews the line way off.
3 minutes: Removing 0,0 and changing the graph range manually. Oh, looks like the outlier didn’t do much for the data anyway, and the line is off entirely.
15 minutes: Trying various fixes before noticing that it’d be best to remove the flip-by-y before making changes, since the flip complicates things.
5 minutes: Rewriting the line of code that draws the graph and remaking the arguments for the function from scratch. No dice.
20 minutes: Various remedies, all of which fail miserably, before sitting down to think and realizing that expanding the graphing range messes with the scale, so I need to add a few correction terms in to shift the line back.
5 minutes: Code-wrangling, which finally gives a line that works.
5 minutes: Drawing an X-axis.
Total time elapsed: 1 hour 45 minutes on a really short piece of code, plus the 2 hours from last night, plus the half an hour I used to write up this article.
Man, where’s the Ballmer peak when you need it?
-PKTT