Literate programming is a programming paradigm introduced by Donald Knuth in which a computer program is given an explanation of its logic in a natural language , such as English, interspersed with snippets of macros and traditional source code , from which compilable source code can be generated. The literate programming paradigm, as conceived by Knuth, represents a move away from writing computer programs in the manner and order imposed by the computer, and instead enables programmers to develop programs in the order demanded by the logic and flow of their thoughts. Literate programming LP tools are used to obtain two representations from a literate source file: one suitable for further compilation or execution by a computer, the "tangled" code, and another for viewing as formatted documentation , which is said to be "woven" from the literate source. Literate programming was first introduced by Knuth in
|Published (Last):||6 May 2018|
|PDF File Size:||18.65 Mb|
|ePub File Size:||10.76 Mb|
|Price:||Free* [*Free Regsitration Required]|
Literate programming is a programming paradigm introduced by Donald Knuth in which a computer program is given an explanation of its logic in a natural language , such as English, interspersed with snippets of macros and traditional source code , from which compilable source code can be generated. The literate programming paradigm, as conceived by Knuth, represents a move away from writing computer programs in the manner and order imposed by the computer, and instead enables programmers to develop programs in the order demanded by the logic and flow of their thoughts.
Literate programming LP tools are used to obtain two representations from a literate source file: one suitable for further compilation or execution by a computer, the "tangled" code, and another for viewing as formatted documentation , which is said to be "woven" from the literate source.
Literate programming was first introduced by Knuth in The main intention behind this approach was to treat a program as literature understandable to human beings. This approach was implemented at Stanford University as a part of research on algorithms and digital typography. This implementation was called " WEB " by Knuth since he believed that it was one of the few three-letter words of English that hadn't already been applied to computing.
Literate programming is writing out the program logic in a human language with included separated by a primitive markup code snippets and macros. Macros in a literate source file are simply title-like or explanatory phrases in a human language that describe human abstractions created while solving the programming problem, and hiding chunks of code or lower-level macros.
These macros are similar to the algorithms in pseudocode typically used in teaching computer science. These arbitrary explanatory phrases become precise new operators, created on the fly by the programmer, forming a meta-language on top of the underlying programming language. A preprocessor is used to substitute arbitrary hierarchies, or rather "interconnected 'webs' of macros",  to produce the compilable source code with one command "tangle" , and documentation with another "weave".
The preprocessor also provides an ability to write out the content of the macros and to add to already created macros in any place in the text of the literate program source file, thereby disposing of the need to keep in mind the restrictions imposed by traditional programming languages or to interrupt the flow of thought. According to Knuth,   literate programming provides higher-quality programs, since it forces programmers to explicitly state the thoughts behind the program, making poorly thought-out design decisions more obvious.
Knuth also claims that literate programming provides a first-rate documentation system, which is not an add-on, but is grown naturally in the process of exposition of one's thoughts during a program's creation. This differs from traditional documentation, in which a programmer is presented with source code that follows a compiler-imposed order, and must decipher the thought process behind the program from the code and its associated comments.
The meta-language capabilities of literate programming are also claimed to facilitate thinking, giving a higher "bird's eye view" of the code and increasing the number of concepts the mind can successfully retain and process.
Applicability of the concept to programming on a large scale, that of commercial-grade programs, is proven by an edition of TeX code as a literate program. Knuth also claims that Literate Programming can lead to easy porting of software to multiple environments, and even cites the implementation of TeX as an example. Literate programming is very often misunderstood  to refer only to formatted documentation produced from a common file with both source code and comments — which is properly called documentation generation — or to voluminous commentaries included with code.
This is the converse of literate programming: well-documented code or documentation extracted from code follows the structure of the code, with documentation embedded in the code; while in literate programming, code is embedded in documentation, with the code following the structure of the documentation.
This misconception has led to claims that comment-extraction tools, such as the Perl Plain Old Documentation or Java Javadoc systems, are "literate programming tools". However, because these tools do not implement the "web of abstract concepts" hiding behind the system of natural-language macros, or provide an ability to change the order of the source code from a machine-imposed sequence to one convenient to the human mind, they cannot properly be called literate programming tools in the sense intended by Knuth.
Knuth came up with an 8-pages long monolithic listing that was published together with a critique by Douglas McIlroy of Bell Labs. McIlroy praised intricacy of Knuth's solution, his choice of a data structure Frank M.
Liang's hash trie , but noted that more practical, much faster to implement, debug and modify solution of the problem takes only six lines of shell script by reusing standard Unix utilities. McIlroy concluded: . Knuth has shown us here how to program intelligibly, but not wisely. I buy the discipline. I do not buy the result. He has fashioned a sort of industrial-strength Faberge egg—intricate, wonderfully worked, refined beyond all ordinary desires, a museum piece from the start.
McIlroy later admitted that his critique was unfair, since he criticized Knuth's program on engineering grounds, while Knuth's purpose was only to demonstrate the literate programming technique. A classic example of literate programming is the literate implementation of the standard Unix wc word counting program. The same example was later rewritten for the noweb literate programming tool.
The following snippet of the wc literate program  shows how arbitrary descriptive phrases in a natural language are used in a literate program to create macros, which act as new "operators" in the literate programming language, and hide chunks of code or other macros. Actually, writing out the expanded source code can be done from any section or subsection i.
The unraveling of the chunks can be done in any place in the literate program text file, not necessarily in the order they are sequenced in the enclosing chunk, but as is demanded by the logic reflected in the explanatory text that envelops the whole program. Macros are not the same as "section names" in standard documentation. Literate programming macros can hide any chunk of code behind themselves, and be used inside any low-level machine language operators, often inside logical operators such as " if ", " while " or " case ".
This is illustrated by the following snippet of the wc literate program. In fact, macros can stand for any arbitrary chunk of code or other macros, and are thus more general than top-down or bottom-up "chunking", or than subsectioning. Knuth says that when he realized this, he began to think of a program as a web of various parts. The documentation for a literate program is produced as part of writing the program. Instead of comments provided as side notes to source code a literate program contains the explanation of concepts on each level, with lower level concepts deferred to their appropriate place, which allows for better communication of thought.
The snippets of the literate wc above show how an explanation of the program and its source code are interwoven. Such exposition of ideas creates the flow of thought that is like a literary work. Knuth wrote a "novel" which explains the code of the interactive fiction game Colossal Cave Adventure. The first published literate programming environment was WEB , introduced by Knuth in for his TeX typesetting system; it uses Pascal as its underlying programming language and TeX for typesetting of the documentation.
Knuth had privately used a literate programming system called DOC as early as He was inspired by the ideas of Pierre-Arnoul de Marneffe.
There are various other implementations of the literate programming concept some of them don't have macros and hence violate the order of human logic principle :. Again, I did this in the interests of simplicity, because I noticed that most applications of multiple parameters could in fact be reduced to the one-parameter case.
For example, suppose that you want to define something like In other words, the name of one macro can usefully be a parameter to another macro.
This particular trick makes it possible to My original idea was that WEB would be merely a tool for documentation, but I actually found that my WEB programs were better than the programs I had been writing in other languages. And the fact that there's no need to be hung up on the question of top-down versus bottom-up, since a programmer can now view a large program as a web, to be explored in a psychologically correct order is perhaps the greatest lesson I have learned from my recent experiences.
But as time went on, I've become extremely pleased with the name, because I think that a complex piece of software is, indeed, best regarded as a web that has been delicately pieced together from simple materials. We understand a complicated system by understanding its simple parts, and by understanding the simple relations between those parts and their immediate neighbors. If we express a program as a web of ideas, we can emphasize its structural properties in a natural and satisfying way.
From Wikipedia, the free encyclopedia. The number of lines in a file is The present chunk , which does the counting , was actually one of the simplest to write. We look at each character and change state if it begins or ends a word. The grand totals must be initialized to zero at the beginning of the program. If we made these variables local to main, we would have to do this initialization explicitly; however, C globals are automatically zeroed.
The Computer Journal. British Computer Society. Retrieved January 4, Journal of Statistical Software. Archived PDF from the original on 30 May Retrieved 30 May But after gaining experience with WEB, I have come to realize that there is no need to choose once and for all between top-down and bottom-up, because a program is best thought of as a web instead of a tree. A hierarchical structure is present, but the most important thing about a program is its structural relationships.
A complex piece of software consists of simple parts and simple relations between those parts; the programmer's task is to state those parts and those relationships, in whatever order is best for human comprehension not in some rigidly determined order like top-down or bottom-up. Retrieved Knuth , Literate Programming . Sewell, Wayne Van Nostrand Reinhold.
Knuth, Donald E. Literate Programming. Guari, Eitan M. McGraw Hill. University of Aalborg. Schulte, Eric January Mall, Daniel. Donald Knuth. Categories : Literate programming Computer-related introductions in Hidden categories: Webarchive template wayback links CS1 errors: missing periodical Articles with example code.
Supports TeX equations. Compatible with Vim literate. It can translate a single LP source into any number of code files. It does it in a single invocation; it does not have separate weave and tangle commands.
I believe that the time is ripe for significantly better documentation of programs, and that we can best achieve this by considering programs to be works of literature. Hence, my title: "Literate Programming. Let us change our traditional attitude to the construction of programs: Instead of imagining that our main task is to instruct a computer what to do, let us concentrate rather on explaining to human beings what we want a computer to do. The practitioner of literate programming can be regarded as an essayist, whose main concern is with exposition and excellence of style.
Learn To Program With Literate Programming
I started finding myself making excuses for my lack of ability. Programming books are universally boring. Typically they also like to mention that the arcana will be demystified in another chapter. One where I could interact with the example code as I typed it. I wanted to end up with a full understanding before I pressed that run button for the first time, not after. So, for example, a literate program would start with a section like this:. Well, Wikipedia explained it better.