3. Self-Documenting Programs

Defining self-documenting programs

Self-documenting programs are not used nearly as frequently as structures like linked lists. This is primarily because they do not have as strong a practical value - the concept serves much better as a form of recreational programming. And in that regard, they provide an interesting intellectual challenge.

The idea is simple: write a computer program that, when run, produces itself as output. For now, we will use this as the definition of a self-documenting program, although later we will see how we can stretch this definition to produce examples that seem unsatisfactory.

The complexities

Because the idea is so simply stated, one might think that there would be a simple solution, that one could just sit down at a computer and start writing a program to perform this task. In most cases, however, actually implementing a self-documenting program is far from simple. In fact, in a certain sense, it is impossible to ever start writing one! Let us examine why.

Because we are not dealing with complex data or abstractions, we would traditionally employ a top-down design to write this program. The idea behind top-down design is to begin with a very high level description of the solution, and iteratively refine this description until you arrive at the level of actual program statements. So, let us attempt this with the given problem. We begin with a simple (if somewhat ambitious) program definition, with the section to be refined written in italics:

    main() {
        print myself out.
The next step would be to refine the "print myself out" section of the program. But with what? There is no straightforward way to insert code into this section of the program, because whatever code gets used in the end, it has to perform a double duty. It has to not only function properly, that is to say actually print itself out, it also has to specify what needs to be printed out, namely itself! In a sense, the code that gets inserted here needs to anticipate its own structure before it even gets written. It has to be the program and the description of the program. It has to be the use and the mention. It has to form a closed loop on itself. In this sense, you could never just start writing such a program. You would have to compose the entire program at once in a conceptual leap.
[back] [up] [right]