|Ada 95 Quality and Style Guide||Chapter 3|
3.3.6 Statement Comments
Minimize comments embedded among statements.
Use comments only to explain parts of the code that are not obvious.
Comment intentional omissions from the code.
Do not use comments to paraphrase the code.
Do not use comments to explain remote pieces of code, such as subprograms called by the current unit.
Where comments are necessary, make them visually distinct from the code.
The following is an example of very poorly commented code:... -- Loop through all the strings in the array Strings, converting -- them to integers by calling Convert_To_Integer on each one, -- accumulating the sum of all the values in Sum, and counting them -- in Count. Then divide Sum by Count to get the average and store -- it in Average. Also, record the maximum number in the global -- variable Max_Number. for I in Strings'Range loop -- Convert each string to an integer value by looping through -- the characters which are digits, until a nondigit is found, -- taking the ordinal value of each, subtracting the ordinal value -- of '0', and multiplying by 10 if another digit follows. Store -- the result in Number. Number := Convert_To_Integer(Strings(I)); -- Accumulate the sum of the numbers in Total. Sum := Sum + Number; -- Count the numbers. Count := Count + 1; -- Decide whether this number is more than the current maximum. if Number > Max_Number then -- Update the global variable Max_Number. Max_Number := Number; end if; end loop; -- Compute the average. Average := Sum / Count;
The following is improved by not repeating things in the comments that are obvious from the code, not describing the details of what goes in inside of Convert_To_Integer, deleting an erroneous comment (the one on the statement that accumulates the sum), and making the few remaining comments more visually distinct from the code.Sum_Integers_Converted_From_Strings: for I in Strings'Range loop Number := Convert_To_Integer(Strings(I)); Sum := Sum + Number; Count := Count + 1; -- The global Max_Number is computed here for efficiency. if Number > Max_Number then Max_Number := Number; end if; end loop Sum_Integers_Converted_From_Strings; Average := Sum / Count;
The improvements shown in the example are not improvements merely by reducing the total number of comments; they are improvements by reducing the number of useless comments.
Comments that paraphrase or explain obvious aspects of the code have no value. They are a waste of effort for the author to write and the maintainer to update. Therefore, they often end up becoming incorrect. Such comments also clutter the code, hiding the few important comments.
Comments describing what goes on inside another unit violate the principle of information hiding. The details about Convert_To_Integer (deleted above) are irrelevant to the calling unit, and they are better left hidden in case the algorithm ever changes. Examples explaining what goes on elsewhere in the code are very difficult to maintain and almost always become incorrect at the first code modification.
The advantage of making comments visually distinct from the code is that it makes the code easier to scan, and the few important comments stand out better. Highlighting unusual or special code features indicates that they are intentional. This assists maintainers by focusing attention on code sections that are likely to cause problems during maintenance or when porting the program to another implementation.
Comments should be used to document code that is nonportable, implementation-dependent, environment-dependent, or tricky in any way. They notify the reader that something unusual was put there for a reason. A beneficial comment would be one explaining a work around for a compiler bug. If you use a lower level (not "ideal" in the software engineering sense) solution, comment on it. Information included in the comments should state why you used that particular construct. Also include documentation on the failed attempts, for example, using a higher level structure. This kind of comment is useful to maintainers for historical purposes. You show the reader that a significant amount of thought went into the choice of a construct.
Finally, comments should be used to explain what is not present in the code as well as what is present. If you make a conscious decision to not perform some action, like deallocating a data structure with which you appear to be finished, be sure to add a comment explaining why not. Otherwise, a maintainer may notice the apparent omission and "correct" it later, thus introducing an error.
See also Guideline 9.3.1 for a discussion of what kind of documentation you should provide regarding tagged types and redispatching.
Further improvements can be made on the above example by declaring the variables Count and Sum in a local block so that their scope is limited and their initializations occur near their usage, e.g., by naming the block Compute_Average or by moving the code into a function called Average_Of. The computation of Max_Number can also be separated from the computation of Average. However, those changes are the subject of other guidelines; this example is only intended to illustrate the proper use of comments.
|< Previous Page||Search||Contents||Index||Next Page >|