If you've spent much time programming in industry, you've probably seen comments like this:
/***************************************
* Function: Frobnicate
* Parameters:
* frob - a pointer to a Frob
* options - a pointer to a FrobnicationOptions
* Returns:
* a boolean: true if frobnication succeeded, false otherwise
* Description:
* Frobnicate the given frob.
* Logic:
* 1. Check for valid frob
* 2. Check for valid options
* 3. Frobnicate the frob
* 4. Clean up
******************************************/
bool Frobnicate(Frob *frob, FrobnicationOptions *options) {
...200 lines bearing no resemblance to the “Logic” section above...
}
Such formal comments could in principle contain useful information, but in practice they hardly ever do. Usually the name and parameters simply repeat information from the signature, the return value and description are easily guessable, and the Logic section is useless if not completely wrong. I don't think I've ever seen one that includes the sort of information the maintainer is most likely to want. (Does Frobnicate
write to options->frobnication_output
? What does it return if the options
say to skip this frob? Does it lock frob->mutex
, or does the caller have to do that?) Most simply waste space and prevent the reader from seeing other code.
Why are useless formal comments so common? I suppose they're often written by people who want to comment their code but don't understand what information is useful, and who, by following the form, can crank out lots of comments they don't realize are useless. But why do some people not only write them but even advocate them, and include them in style guides?
There is a context where these comments make sense: assembly code. When there are no parameter lists or type declarations or even variable names, the information they convey will have to be included somewhere else, because it's vital to maintenance. When the body of a function consists of many pages of inscrutably low-level instructions, an introductory explanation of the logic can be a big help (although I still prefer having it sprinkled among the code, where it helps with navigation and is more likely to be kept up to date.) So prescriptions to include these things in comments are not completely out of the blue. They're based on what made sense, decades ago, for languages that couldn't express this information in any other way. Naturally, they've become entrenched in some programmers' concepts of good style, and are applied even in languages where they make no sense.
I wonder how many of our widely accepted stylistic rules are similarly out of date or misapplied. The widespread misuse of Hungarian notation qualifies, but that's no longer popular. There are a lot of prescriptions for object-oriented style that make no sense in functional languages, but they're seldom applied there. There are some language-specific archaisms, like writing typedef struct Foo_ { ... } Foo;
in C++, but I don't think that's actively promoted except for code that has to also be valid as C.
What stylistic prescriptions are still widely accepted today, even where they don't make sense?
Tab characters, perhaps? >:)
ReplyDeleteI once had the "pleasure" of maintaining a code base which was strewn with the "formal comments" of the useless type you describe. But as a sort of bonus, about 80% of them had no content. The only "field" that was filled out was "Function:".
This leads me to believe that at least one of the reasons for their relative popularity despite their uselessness is the existence of development tools that create boilerplate for them.
Tab characters are a perfectly reasonable device perverted by historical accident; their usage as a semantic indicator of an indentation is perfectly valid; it is those who believed that tabs were merely a compacting of 8 spaces (false even if you incorrectly assumed that everyone uses, or should use, the same tab width: tabs, of course, are "conventionally" displayed by moving to the next column divisible by 8), and the following usage of a tab to replace all sequences of 8 spaces, including for alignment purposes, that destroyed the tab's reputation.
ReplyDelete