Literate Programming
Programming methodology, introduced by Donald Knuth, in which programs are written as explanatory documents for people with the code embedded in them.
Literate programming treats a program as a piece of literature addressed to human readers. Instead of writing code in the order a compiler requires and adding comments as an afterthought, the programmer writes an essay that explains the program's logic in natural language, introducing code fragments in whatever order best suits the explanation. Tools then process this single source in two ways: one extracts and reassembles the code so that it can be compiled, and the other produces a typeset document combining the prose and the code, complete with cross-references and an index.
Donald Knuth introduced the approach in a 1984 article in The Computer Journal and developed a system called WEB, whose TANGLE program produced compilable Pascal code and whose WEAVE program produced documentation formatted with his typesetting system TeX. Knuth wrote TeX itself as a literate program and published it as a book. Later tools generalized the idea: CWEB applied it to the C language, and noweb, created by Norman Ramsey, made literate programming independent of any particular programming language.
Strict literate programming never became mainstream, partly because it demands sustained writing effort and partly because everyday tools such as debuggers work with the extracted code rather than the source document. Its influence is wide, however. Documentation generators build manuals from code comments; Org mode in GNU Emacs embeds executable code blocks in documents; computational notebooks such as Jupyter interleave prose, code and results. All reflect its premise. The idea resonates with research reproducibility and with personal knowledge management, where explanation and working material are kept together in Markdown or similar formats.
Key points
- Literate programming presents a program as an explanatory document for people, with code embedded in the prose.
- Donald Knuth introduced the method in 1984 and used his WEB system to write TeX.
- Tools tangle the source into compilable code and weave it into a typeset document.
- Computational notebooks such as Jupyter reflect literate programming's idea of mixing explanation and code.
Related entries
Donald Knuth
American computer scientist who wrote "The Art of Computer Programming", created the TeX typesetting system and shaped the analysis of algorithms.
Jupyter
Open-source project for interactive computing whose notebooks combine live code, results, equations and narrative text in one document.
Markdown
Lightweight markup language that uses plain-text conventions such as asterisks and pound signs to format text that converts easily to HTML.
GNU Emacs
Extensible, customizable free text editor started by Richard Stallman in 1984, built around an Emacs Lisp interpreter.
Personal Knowledge Management
Practices and tools individuals use to capture, organize, connect and retrieve information and ideas for learning, thinking and creative work.
Compiler
Program that translates source code written in one programming language, usually a high-level one, into another form such as machine code.
Code Review
Practice in which developers examine each other's source code changes to find defects, share knowledge and keep the codebase consistent.