Literate programming is a combined coding/documentation method, supported by preprocessing tools that extract code and documentation files from a single source file. The main idea behind literate programming is that many programs are more often read by humans than executed, so it's vitally important to make them readable and to ensure that documentation remains consistent with the code. Another key idea is that the goal of presenting programs in segments arranged in a logical order (rather than the order required by the compiler) actually helps the programmer to design good programs in the first place. So one should not first write the code and then start reorganizing it for purposes of documentation; instead it's much better to code and document at the same time. In printed documentation produced through literate programming and its extraction tool, code is presented in "parts" numbered according to the page number on which they appear, with parts on the same page distinguished by appending a letter to the number. Parts are referenced in other code by name and number. The order in which the parts appear in the documentation does not have to be the same order in which they must appear in the code source file(s) according the programming language rules. The details are more easily understood by looking at an example, such as the small program presented below. Some literate programming tools are also able to produce the documentation as hyperlinked PDF pages, in which case the part references contain hyperlinks to the place where the part was defined, and part definitions contain hyperlinks to the places where the parts are used. The printed page form of presentation is supported by P. Briggs' Nuweb tool, and hyperlinked PDF is supported in a very similar way by the Pdfnuweb extension of Nuweb developed by R. Loos. When Nuweb or Pdfnuweb processes a Nuweb source file, it simultaneously constructs one or more code files for input to a language processing system (typically a compiler, but a utility such as make is also frequently the code target), and one or more documentation files for input to the latex or pdflatex document processor. Both the concept of literate programming and the first tools
supporting it were introduced by D. E. Knuth, who invented the
approach while constructing and documenting source code of his TeX
documentation processing system (on top of which LaTeX was later
developed by L. Lamport as a preprocessor). Knuth's original literate
programming system, Web, has separate tools for extracting the code
and the documentation, called
There is further information on Nuweb now in the Resources section. |