Lterate programming
lastmod 2018-08-17created 2015-06-181 edits
Literate programming is a programming methodology that, when programming, focuses on creating code that is easy for humans to understand rather than code that a computer can compile. In other words, the aim is to program the way one writes a document, so that people can read and understand it. Since the goal is 'to make a program readable as if reading a work of literature', it was named 'literate programming'.
In literate programming, the documentation for a program and its source code are contained in a single source file, and a special program is used to extract the documentation and the source code separately. The source code is divided into units commonly called 'chunks', and the source file explains in detail how each chunk works. The chunks may be arranged in an order different from that of the actual source code; they are arranged in the order in which the documentation wants to explain them. A separate program is used to turn it into a form that a compiler or interpreter can process.
Origin
The first literate programming environment was WEB, developed by Donald Knuth in 1981 while creating the TeX typesetting system. WEB uses Pascal as its programming language and TeX for documentation.
Knuth wrote the entire TeX source in literate programming form, and documented and published it as Volume B, 《TeX: The program》, of his five-volume book 《Computers and Typesetting》. Knuth had used a literate programming environment he named DOC since 1979. Knuth himself said he began this work inspired by the ideas of Pierre Arnoul de Marneffe. To make up for the drawback of WEB, which could only use Pascal, Knuth and Levy created CWEB. The features of CWEB are as follows.
- C, C++, Java, etc. can be used as the programming language.
- Runs on most operating systems.
- Can produce not only TeX but also PDF documents.
Other literate programming tools include noweb, FunnelWeb, etc.
Implementation
Among literate programming tools, Leo is the most widely used and most powerful. Leo has built-in noweb and CWEB processors and also provides an editor with an easy-to-use interface. It runs on various systems such as Windows, Linux, and Mac OS X. The Leo manual also includes an easy-to-understand explanation of literate programming.
Similar to literate programming, there is a method of extracting documentation from program comments using documentation generators such as the following.
However, these methods fall short of literate programming in the strict sense.
- They can describe a program by explaining its functions and parameters, but cannot document the source code as a whole.
- The source files used in most literate programming environments take a third form that is neither a documentation file nor source code. In programs like the above, however, they take the form of source code with explanations added as comments.
- Because source files in literate programming take this third form, the author can arrange program chunks in any desired order when explaining the program. In tools like the above, however, explanations are attached to the source code as comments, so the order of explanation has no choice but to follow the order of the source code.