views:

283

answers:

5

Where is it customary to write the in-code documentation of classes and methods?

Do you write such doc-blocks above the corresponding class/method in the header (.hpp) file, or within the source (.cpp) file?

Is there a widely respected convention for such things? Do most C++ projects do it one way rather than the other?

Or should documentation be written on the two sides (i.e. in the .hpp and the .cpp files), maybe with one short description one one side and a longer one on the other side?

Most importantly, are there any practical considerations that makes it more convenient to write it one way rather than the other way ? (E.g. the use of automatic documentation parsers and generators like Doxygen...)

+13  A: 

If you make a library, you typically distribute the compiled library and the header files. This makes it most useful to put documentation in the header files.

Sjoerd
+18  A: 

Both. Describe the API design and usage in the header: that's your public interface for clients. Describe the implementation alternatives / issues and decisions in the implementation: that's for yourself - later - other maintainers/enhancers, even someone reviewing the design as input to some next-gen system years hence.

Comment anything that's not obvious (and nothing that is, unless your documentation tool's too stupid to produce good documentation without).

Avoid putting implementation docs in the headers, as changing the header means makefile timestamp tests will trigger an unnecessary recompilation for client apps including your header (at least in an enterprise or commercial library environment). For the same reason, aim to keep the header documentation stable and usable - good enough that you don't need to keep updating it as clients complain or ask for examples.

Tony
Usually, I write my documentation before I write the function definitions. That is, unless there is a typo, having to change the documentation usually implies a change in the function as well, so you have to recompile things anyway.
ereOn
@ereOn: I think that @Tony suggestion still holds good: if it is part of the interface it is in the header, if it is an implementation detail (*using container A instead of B for reason X*) then a change in the implementation code requires a change in the implementation doc, but users should not even notice.
David Rodríguez - dribeas
"Comment anything that's not obvious (**and nothing that is**[...])." Sound advice. The fewer comments, the better. Unit tests are also a great way to document the usage of an API.
Johnsyweb
+3  A: 

Again, both. As for the public documentation, it is nice to be in the .h with a format extractable with Doxygen, for example, as other commented. I like very much the Perl way of documenting things. The .pl (or .pm) file includes documentation in itself that can be extracted using a tool similar to what Doxygen does for C++ code. Also, Doxygen allows you to create several different pages, that allow you to include user manuals, etc., not just documenting the source code or API. I generally like the idea of a self-contained .h/.hpp file in the philosophy of literate programming.

Diego Sevilla
+1  A: 

I personally like documentation in the header files. However, there are some, that believe that documentation should be put in the source files. The reason being, that when something changes, the documentation is right there reminding you to update it. I somewhat agree, as I personally have forgotten to update the doxygen comments in the header when I changed something in the source files.

I still prefer the doxygen comments to be in the header file for aesthetics reasons, and old habits are hard to change. I've tried both and doxygen allows documenting either in source or header files no problem.

Dennis Miller
+1  A: 

Most importantly, are there any practical considerations that makes it more convenient to write it one way rather than the other way ?

Suppose that you want to add a clarification to one of your comments without changing the code. The problem is that your build system will only see that you changed the file, and unnecessarily assume that it needs to be recompiled.

If the comments are in the .cpp file, it will just recompile that one file. If the comments are in the .hpp file, it will recompile every .cpp file that depends on that header. This is a good reason to prefer having your comments in the .cpp files.

(E.g. the use of automatic documentation parsers and generators like Doxygen...)

Doxygen allows you to write your comments either way.

dan04