I've been thinking about literate programming lately.
LP is the brainchild of legendary algorithm-master Donald Knuth, one of our field's undisputed deep thinkers. For the past 50 years Knuth has been toiling on his masterwork, The Art of Computer Programming, an encyclopedic exploration of algorithms, languages, and compilers. Knuth has finished just three of the planned seven volumes in his first 50 years of work, but it's not entirely his fault. He gets distracted. Disappointed with the state-of-the-art in typesetting when he published the first volume, for instance, he took some time off to create TeX and METAFONT.
Knuth introduced LP in an article called “Literate Programming” that appeared in the British Computer Society's Computer Journal in 1984.
The idea behind LP is that developers should write text that describes each aspect of their programs along with the code, in the same file, using a markup language to signal logical structure (headings, subsections, and so on). Special tools make the text visible and invisible, and strip out the text before compilation.
Knuth argued that describing code in prose would lead programmers to think more carefully about what their code did and how it operated. A mental feedback cycle would be established as the programmer wrote the text and the source code, and the result would be better software.
“The practitioner of literate programming,” Knuth wrote, “can be regarded as an essayist, whose main concern is with exposition and excellence of style. Such an author, with thesaurus in hand, chooses the names of variables carefully and explains what each variable means. He or she strives for a program that is comprehensible because its concepts have been introduced in an order that is best for human understanding, using a mixture of formal and informal methods that reinforce each other.”
LP aficionados flame anyone who suggests that LP is just a mechanism for writing better-documented code. They emphasize that the programmer is really writing an essay about the intentions behind the code, and explicitly explaining how those intentions are mapped into source code. It's not just documentation. It's not just a discipline. It is a (dare I say it?) paradigm.
A central assumption behind LP is the notion that programmers can write well-structured essays that explain the why and how of their code. I'm skeptical about that. Going back to college showed me that the ability to write a well-structured essay is surprisingly rare (even at Columbia). Anyone with the ability to organize his thoughts sufficiently to write an essay about their code probably doesn't need help getting the code organized properly.
In other words, literate programming can help only those who least need its assistance.
The tools page at literateprogramming.com seems a bit out-of-date, but it provides a list of LP tools for use with different languages. A quick search of the Web turned up dozens more, most of them free. If you want to get started with LP, there's no reason to hesitate.
But modern operating environments have brought substantial formatting capabilities to source code editors, and I think those capabilities have rendered LP obsolete.
Yes, we should embed more and better documentation in our code. But once we've made that commitment, LP and LP tools don't bring much to the table.
Web recommendation: Computerworld's Preston Gralla is a super-smart guy and I've learned a lot from reading his columns over the years. But I think he's missed the mark with his latest effort. Gralla argues that those who are urging Apple to insist upon better working conditions for the offshore workers who manufacture its products must be prepared to pay more for Apple products. I think Gralla missed an obvious point. Apple could invest more in worker safety and working conditions without raising prices if it were willing to reduce its legendary profit margins somewhat. Apple is a profit machine. In the final quarter of 2011 the company had revenues of $46.33 billion and quarterly net profit of $13.06 billion. The company has $97.6 billion in the bank. Sharing some love with abused workers in overseas sweatshops shouldn't force Apple to raise prices. Gralla's article is here. J.D. says check it out.
J.D. Hildebrand has written hundreds of articles for dozens of publications and online communities dedicated to software development. He feels virtuous today because he spent the morning mopping floors.