This page contains a collection of suggestions for what to think about when writing code-level documentation. The documentation in code falls into two categories, described in separate sections below.

Interface documentation

Interface documentation consists of the header comments for files, classes, and methods. Its purpose is to support abstraction: the creation of reusable modules with simple interfaces that hide complex internals. It should be much easier to use a module than to understand its implementation. The module's documentation describes its interface.

Implementation documentation

These comments are intended to help developers understand how code works internally. It includes documentation in the bodies of methods plus documentation of member variables in classes, which are usually private.

Overall suggestions

The following suggestions apply to both implementation and interface documentation.

Other questions

What about private methods within a class: should they be documented like "implementation"?

Ideally, even private methods should provide encapsulation in the sense of hiding some nasty detail from the rest of the class. In this case, they should be documented in a fashion similar to public methods to reflect this abstraction.

On the other other hand, there will be some cases where there isn't much abstraction in a private method; it's just a way of sharing code between other methods. In this case the private method is just a shared implementation detail, so it's okay for the method header to be somewhat more abbreviated, for example:

    This method is used internally in several places to perform
    the main body of work in copying data out of a Buffer.

Even in this case parameters and result should be documented (but if it's hard to explain them outside the context of the calling method it's okay to keep the documentation brief and vague).

This second case as a necessary evil that we tolerate in situations where it's the best available alternative, but it's better if private methods can fall into the first category where they really have abstraction and the documentation reflects it.