Literate Code in Crista Lopes’s Exercises in Programming Style

& (verbiage overflow)Tue 07 October 2014RSS

Hacker School’s regular Monday night lecture last night was presented by Cristina Videira Lopes, whose new book (Exercises in Programming Style) offers thirty-three Python programs, all to accomplish the same simple task but all written in different programming styles. It was a great talk — one of the best I’ve seen at Hacker School. The code itself is posted, with variations, in a GitHub repository, and the book is ISBN 978-1482227376. One of Lopes’s points in the talk, and this is apparently documented in the book, is the considerable age of some of the programming styles, many of which date to the 1950s and 60s. It is not easy to find good high-level but simultaneously technical accounts of the history of programming — part of the draw I feel to Donald Knuth’s dense Art of Computer Programming is that it is such a trove of information about the history of ideas in this field.

Lopes was asked which style was her favorite, and she named the “Code Golf” style. The last comment on that style was a mild audience rejection of setting terseness above all other considerations. But I think there is more to be said about this style.

The most compelling revision to her original model was contributed by Peter Norvig:

import re, sys, collections

stopwords = set(open('../stop_words.txt').read().split(','))
words = re.findall('[a-z]{2,}', open(sys.argv[1]).read().lower())
counts = collections.Counter(w for w in words if w not in stopwords)
for (w, c) in counts.most_common(25):
    print w, '-', c

It seems to me that one thing that makes the “Code-Golf” version so alluring is its narrative expressiveness. The four statements in this program are a succinct strategy for solving the problem — first get a list of stopwords and gather the words themselves, then count the frequencies of words that are not stopwords, and finally list the most common twenty-five of those. What gives the Code-Golf version so much power is this Knuthian quality of “literate code”.

The tag “Code-Golf” is not exactly a fair description of this program, although I see the names of the styles are meant to be jocular.

[end]

Comments are enabled.