“Exercises in Programming Style” by Cristina Videira Lopes is the best programming book to come along in many years. Casting back over many decades, the only book I can compare it to in terms of actionable value is Steve McConnell’s “Code Complete,” and in terms of approachability and sheer fun, it reminds me of Ted Nelson’s classic “Computer Lib/Dream Machines.”
The book is based on a simple idea: program the same task in the manner of 33 different programming styles, ranging from the “good ol’ times” form of a memory-constrained, identifier-free machine, to modern forms such as Map/Reduce and MVC. The task (determining the frequency of words in a text) is simple enough to generally only require a page of code in the style at hand, but is (surprisingly) sufficient to show each style’s signature characteristics. Chapters are typically shorter than eight pages—including source code—and are easily digestible even in our distraction-heavy world.
(Related: Code.org’s own programming education pursuit)
The idea of a tour of programming approaches might remind you of Bruce Tate’s excellent book “Seven Languages in Seven Weeks,” but perhaps Videira Lopes’ most brilliant decision was to write all of the programs in Python. The language is constant; the style is different. Python is easy enough for any developer to read and flexible enough to demonstrate all the styles, at least in a minimalistic way.
Dataflow, for instance, is presented in the “Spreadsheet” style and is given barely five pages—no more or less than the “Code Golf” style. Of course, dataflow is a concept that’s more important and more complex than Code Golf, but avoiding such judgments and lectures is precisely the point of the book. Personally, I detest Code Golf and think it promotes terrible, horrible, really very bad habits. But I can put that aside for a few minutes and recognize the grotesque achievement of a single line applying “a list comprehension, over a regular expression, after a couple of file system operations and a couple of tests!” Aside from Code Golf (and, probably, “Monolithic”), none of the styles are inherently bad.
Many (most) programming styles are inappropriate at the scale of the term-frequency problem, but Videira Lopes supplements every piece of code with a brief discussion of the constraints that give rise to the style, tips on the associated jargon, and references for further reading. So one reader might be inspired to learn more about “idempotence” and “side effects,” while another might investigate “coroutines” or “the actor model.” But both readers can undoubtedly read and enjoy the short, accessible chapter in which she illustrates the various terms.
The delight of the text comes from the growing familiarity with the problem and the boilerplate of Python file-reading operations, which disappears into the background after only a few chapters. Just 33 pages into the book, one sees the structured “Cookbook” style, which is not at all a bad piece of code—the intent and structure is clear, and while there’s no error-handling, it would be easy to debug as a script for personal use.
The programs on the next 233 pages are often a little harder to take in at a glance, with each suggesting a different focus and inviting the reader to agree or disagree with that suggestion (or, even better, to reflect upon times when the style would be appropriate and when it would be a mistake). Every programmer will find some styles that trigger a happy recognition and others that cause an intriguing confusion. Every chapter is a delight.