David's Coding Blog

Generate documentation from code

I think every software developer has thought about the best way to document the project.
Should you write it outside of the repository in a wiki?
Or should you put it in a doc folder within the repository?

Manual documentation

A README.md file is a simple and fast way to document e.g. the projects structure, steps to set it up, or the features it supports. For more visual subjects, pictures or mermaid diagrams can be added. Maybe more detailed documentation is placed outside of the repository in a wiki.

It often happened to me that I forgot to update the documentation after I changed the behavior or added a new feature. Then, I had to make another commit to fix that.

Automatic documentation

I assume that tools exist for most programming languages that can generate documentation, e.g., a website, based on the source code. For PHP, for example, there are phpDocumentor and Scramble, among others. These tools can be used in a CI/CD pipeline to automatically generate documentation after each code change.

Hybrid approach

In my opinion, the best solution for projects that want good documentation is a mix of documentation written by a developer or, nowadays, an AI assistant and documentation generated from the source code itself.

Example from my PHP interpreter

In my project QIQ, an PHP interpreter written in the Go, that approach is used. The README.md is manually maintained by me. Most of the documentation located in the doc/ folder is generated with the "doc-gen" tool in this project.

The generated documentation:

Of all the features of the documentation generator, I am most proud of the dependency graph for the packages. This shows all Go packages of the QIQ project and how they are dependent on each other.

Dependency graph of all Go packages of the QIQ project


Changelog