32

In the header file containing the forward declarations, or in the .cpp file containing the implementations, like this?

//header.h
/* About foo() */
void foo();
/* About bar() */
int bar();
/* About A */
class A
{
public:
    /* About A's constructor */
    A();
    ....
}

Or

//implementation.cpp
/* About foo() */
void foo()
{
    ...
    ...
}
/* About bar */
int bar()
{
    ...
}
/* About A's constructor */
A::A()
{
    ...
}
Bakuriu
  • 98,325
  • 22
  • 197
  • 231
wrongusername
  • 18,564
  • 40
  • 130
  • 214

6 Answers6

41

For usage information, it's nicer to put into the header. That's where people would look first.

The documentation is really successful if no one has to examine your .cpp file to figure out how the component should be used.

Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
  • 2
    +1. And if you're distributing your library or something in a non-open license, you trivially distribute the code documentation with the headers. – Travis Gockel Dec 04 '10 at 18:59
  • 29
    The documentation is *really* successful if I don't have to examine the header either. – jalf Dec 04 '10 at 19:08
6

As far as I know, every time you change something in the .h file, it causes all files that have included that header file to be recompiled. For this reason, I put most of my comments in the .cpp files.

Ray Zhou
  • 716
  • 10
  • 22
5

For C++, I put "documentation comments" in both the cpp and h.

The cpp contains the code, and has documentation comments on every main code element (classes, methods, etc) that describe them - typically with doxygen or Documentation XML comment format (although I don't tend to generate external docs, I find it useful to stick to a standardised format that can be extracted to external tools if/when I decide I want that). This is comprehensive documentation that explains exactly how a caller should use a method, and also any design details that will need to be understood by anyone intending to modify the code, so they understand the intent, "contract", and any important things to understand about the operation of the code. (I've written an addin for Visual Studio, AtomineerUtils, that makes creating and updating these comments quick and easy, so it's really not much effort to document things like this consistently and comprehensively)

My header is treated as a summary (both for the compiler and myself) - it uses a single-line // comment that briefly describes each method. This gives more information than the (hopefully relatively self-documenting) method/parameter names, but a lot less than the detailed documentation in the cpp. Think of it as a summary or reminder that saves you looking at the actual implementation to get enough details to use the method most of the time.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
3

If you're using some sort of automatic documentation generator, comments should go wherever it tells you they should go.

Otherwise, I put general "this function does X" comments next to the function definition rather than the function declaration (unless, of course, the function is declared at its definition).

Because function declarations can be a bit bulky, putting documentation in the header file usually makes it much easier to view all comments pertaining to a given class at once, increasing the other developers' understanding of that class at a glance.

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
  • "Unless, of course, the function is declared at its definition"... in which case you _don't_ put it next to the function definition? Where do you put it then? ;) – leviathanbadger Sep 07 '16 at 20:50
3

You should put your comments in the declaration of the files, i.e., in the header or interface file, the one ending with .h extension.

Probably, for distribution, you are going to ship the header files directly and the .cpp ones in binary form, i.e., not readable, so if you expect someone to read your comments, they should be inside the header file.

There are also implementation documentation, that only has its natural place in the .cpp file.

In any case, it is better to use a documentation tool, and learn the two or three Javadoc usefult tags that are so common used. You have to change the opening comment to /// or /**

//header.h
/** @brief About power()
    @see lpower
    @param x The base to power
    @param y The exponent, number of times to multiply base by itself
    @return x^y
*/
int power(int x, int y);
Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • 13
    It was just a simple example. Examples are created to be obvious on purpose. Your attitude is unjustifiable. – Baltasarq Dec 05 '10 at 19:59
  • 3
    unjustifiable? Now that's an interesting point of view. I don't see anything "unjustifiable" about saying that verbose comments that specify mainly things that should be clear from the function signature are a bad idea. The only thing in your example which *belongs* in a comment is the `@brief` part. The parameters and return value should be obvious from looking at the function signature. My attitude is very justifiable. is yours? – jalf Dec 05 '10 at 21:14
  • 2
    @Jalf, no, no, it is not. You are right. It is me who is wrong. You were holding a very fair argument, with polite manners ("If you want to write a novel, at least do the user a favor and put it in a separate text file, not in the code"). BTW, that's one of the reasons why automatic documentation formats were developed from the very first time: to avoid separate text files noboy bothered to read. – Baltasarq Dec 06 '10 at 12:47
  • 2
    I'd argue that the problem is that this instead gives you documentation that no one bothered to write. ;) – jalf Dec 06 '10 at 13:43
2

If you use a Doxygen, it will generate documentation from either, but if documentation appears in both the header and the implementation, the header takes precedent, so avoid duplication and document the header.

Also the header defines the user interface which is what a user of a class is interested in. Further if you deployed the code as a library or object code, the header is the only source the user will have access to.

Clifford
  • 88,407
  • 13
  • 85
  • 165