« Ask the Internet How It Feels | Main | Irish OpenSolaris Users Group Meeting »
June 08, 2006
Readable code
I had an, "Aha!", moment yesterday, regarding source code readability. I followed a link from Tim Bray's blog, to an analysis by Josh Bloch of a bug in the Java binary search code. In summary, the bug was caused by an overflow in an integer calculation. While reading the explanation of the problem and its solution I realized I had read something like it before and, after a few minutes thinking, I realized it was in the code of Metafont. I had read this in Donald Knuth's book, Literate Programming, a few months earlier. In particular, it was from section 108 of Metafont and the text in question was:
Notice that the computation specifies
(p - q) + pinstead of(p + p) - qbecause the latter could overflow. Let us hope that optimizing compilers do not miss this point; a special variablebe_carefulis used to emphasize the order of computation.
Now when I was reading this I was not studying the code: I was just idly reading it. However, the insight into variable overflow stayed with me. Perhaps it was because the program was written in the literate programming style and, therefore, was explicitly written to be as readable as possible, that I remembered. If so, reading literate programs is a subtly powerful technique for programmer training.
Posted by JohnC at June 8, 2006 11:54 PM