Feed on

Beautiful Code

At the end of August, I picked up a copy of “Beautiful Code” (O’Reilly, edited by Andy Oram and Greg Wilson) that was lying around the Verilab office.  I’d just finished the last Harry Potter book, and I was in the mood for some lighter fare.  I’m about 90% through it, although I skipped a couple of chapters entirely.

I had every intention of really studying and understanding the code excerpts, and I stuck to it for exactly one chapter. After that, I just couldn’t force myself. I’m sure that must say something about me, not sure I want to know what.

However, I enjoyed several chapters. My favorite by far is “A Spoonful of Sewage” by Brian Cantrill. It’s the story of hunting and fixing a lock order bug in the Solaris 8 kernel. I think a big reason for its appeal is it really is written as a story. And along the way, I learned about priority inheritance as a solution to a common resource starvation issue. To me, priority inheritance is definitely a beautiful idea. My second favorite chapter is “The Quest for an Accelerated Population Count” by Henry S. Warren Jr. The divide and conquer approach is another beautiful idea.

Other highlights: “Beautiful Debugging” by Andreas Zeller, “Multidimensional Iterators in NumPy” by Travis E. Oliphant (I almost skipped this one!).

A couple of chapters that I was really looking forward to after browsing the TOC were disappointing: “Subversion’s Delta Editor” by Karl Fogel and ”Distributed Programming with MapReduce” by Jeffrey Dean and Sanjay Ghemawat. I have a feeling I just may not be smart enough to appreciate the beauty here, but, well, I don’t.

The common element of my two favorite chapters is not that the code itself is beautiful per se, but rather the code embodies some non-obvious but elegant algorithm. The beauty is in the idea. So is the term “beautiful code” actually a vacuous, meaningless concept? No, I don’t think so. I think code itself can be beautiful: When a function, a purpose, a meaning, shines clearly, concisely, intuitively through an expression, that expression is beautiful in its own right; beauty in idiom vs beauty in idea. And if a language seems to offer such intuitive expressions regularly, you could conceivably consider that language beautiful.

But having said all that, I simply don’t believe that a crisp, sharp boundary between idea and idiom exists: Function is nothing more than interpretation of form. Language and thought are so deeply intertwined, and any concept can be considered at so many different levels of abstraction.

For example, the priority inheritance algorithm might seem to be far more idea than idiom, while a NumPy slice operator might seem more language bound; but the idea of a multidimensional iterator abstraction surely is not.

Of course, the complexity of “beauty” makes language and thought seem simple by comparison.

Comments are closed.

Work For Verilab