I typically comment blocks of code using /* */
at the top of each section. I don't use inline comments except in special curcumstances (e.g. tricky code) because I feel the comments are "hidden," and the code could be expanded later and comments would have to be micromanaged. For example:
int parseEverything()
{
/* Initialize the parser. */
random_code_here();
still_more_init();
/// (Note: the above line still falls under the init section, even if it's separated by a newline.)
/* Main parser loop. */
while(!done_parsing) {
/* Grab the token. */
if(x) {
/* Preprocessor stuff. */
y();
} else {
/* Normal token. */
z();
}
/* Insert into tree. */
make_tree_node();
insert_into_tree();
/* Move to next token. */
++streamPtr;
/// (Note: the above could be expanded to take up multiple lines, thus
/// if an inline comment were used it'd have to be moved, if
/// you even remember there's a comment there.)
}
clean_up();
}
Of course, if the code is obvious, it shouldn't require a comment, as in clean_up()
. Including the comment doesn't hurt much though, and if expanded later it would be easier to know where to put extra cleanup code.
Using this scheme I find it is easy to follow a function solely by its comments. parseEverything
has three main sections: initialization, the main loop, and clean-up. Within the main loop we grab the token (preprocessor or normal), insert it into the tree, and move on to the next token.
Most likely each section calls for its own [set] of functions, so it's clear you may need to refactor if sections become bulky.
I should add that I format multiline comments as such (my "IDE" (Vim
) inserts the *
at every line for me with the default scripts (for my distro), which is convinient):
/*
* This is a comment which normally would be on one line, but is either split into multiple
* lines for emphasis or so we don't have 160-character hard-to-read comments everywhere.
*/
Also, I comment #else
's and #endif
's:
#ifndef FOO_H
#define FOO_H
#ifdef DEBUG
#else /* DEBUG */
#endif /* !DEBUG */
#endif /* FOO_H */
But that's getting a bit off topic.
(#include
guards don't require the not
(!
) to preceed them because their use is obvious, and it's tradition. =])