Reading Code

Dr. Dobb's Journal September 2003

By Gregory V. Wilson

Greg is a DDJ contributing editor and can be contacted at gvwilson@ddj.com.


Code Reading: The Open Source Perspective
Diomidis Spinellis

Addison-Wesley, 2003
455 pp., $45.00
ISBN 0201799405

This is the 43rd book review I have written for DDJ. According to grep, I have only used the word "wonderful" once in all that time (to describe Stewart Brand's How Buildings Learn). Half an hour after I started reading Diomidis Spinellis's new book Code Reading: The Open Source Perspective, I knew I was going to use the word again.

Code Reading is a wonderful book, and an important one. It doesn't matter whether you're still in high school, or have been programming every day for the last 30 years—this book will teach you things you really need to know. More importantly, I think this book just might change the way programming is taught and learned.

The book's preface says it best:

The reading of code is likely to be one of the most common activities of a computing professional, yet it is seldom taught as a subject or formally used as a method for learning how to design and program.

Spinellis isn't the first person to make this point, but he is the first person I know of to do something about it. In Code Reading, he walks through hundreds of examples of C, C++, Java, and Perl, drawn from dozens of open-source projects such as Apache, NetBSD, and Cocoon. Each example illustrates a point about how programs are actually built. How do people represent multidimensional tables in C? How do people avoid nonreentrant code in signal handlers? How do they create packages in Java? How can you recognize that a data structure is a graph? A hashtable? That it might contain a race condition? And on, and on, real-world issue after real-world issue, each one analyzed and cross-referenced.

But Spinellis doesn't stop there. There's a section on "Additional Documentation Sources," which tells you places to look for information about code that may not be obvious. There's a whole chapter on code-reading tools, which covers regular expressions, common editor features, and tricks you can get your compiler to do to help you find things.

And to cap it all off, there's Chapter 11 (no, not the kind that most dot-commers have become so familiar with). Here, Spinellis sets himself the goal of adding a new SQL date/time function to the hsqldb database in order to return the moon's phase on a given date. This excerpt gives you a feeling for how he goes about it:

Immediately below the array initialization, I see that the strings are inserted into another structure:

for (int i=0; i<keyword.length; i++) {

hKeyword.put(keyword[i], hKeyword);

}

I then search for hKeyword to see what else is included in that structure since most SQL function names are not there. I see that hKeyword is a Hashable instance, local to the class, used for identifying keywords in the wasName method. No other methods add elements to the hKeyword variable, and since it is local to the class, there is no need to further examine it. However, one use of hKeyword provides me with my next lead...

Of course, no book is perfect. While there is some coverage of Windows-specific issues, for example, the book (like the open-source projects on which it is based) is primarily UNIX oriented. Some of the diagrams showing the structure of various source trees are too detailed for my eyes and, well, that's about it, really. The writing is crystal clear, and completely free of hyperbole and strained jokes; the index (including a code index) and bibliography are rock-solid; and gee, I'm back to praising it, aren't I? Go figure.

According to the preface, Spinellis is already working on a second volume, which will cover interfacing, internationalization, commonly used libraries, low-level code, and other topics. If these two books get the attention they deserve—in particular, if college instructors start using them as texts—I think they could do more to change our profession for the better than any of the bandwagons that have rolled by in the last 20 years.

DDJ