The Psychology of Quality and More |
CHAPTER 4 : Commenting
4.8 Commenting dataDiscovering what uncommented code does can be difficult, although by careful tracing a fair guess can often be made. Data, on the other hand, does not do anything. Actions are performed on it and because of it, but not by it. Discovering the purpose of uncommented data can be difficult, especially if it is badly named and widely used. For commenting purposes then, it can be considered as 'complex code', and block and trailing comments used to describe it. 4.8.1 Comment on usageData is ideally described by the symbol name given to it. Unfortunately, however, even when the name makes complete sense in the context of the program, the reader can still be left asking, "Yes, but what is it for?". Take, for example, the following:
int StdFlex;
This may read as 'standard flex', which may be an intermediate factor in an calculation. It is difficult to understand what this item is for when it is defined. It may also be difficult to determine what it is for in use.
StdFlex += FlexFactor( Plastic.Flex ); ---------------------------------------------------------------------- A comment on a data definition helps clarify the intent for its usage, and can also help the reader to understand its actual usage.
int StdFlex; /* Intermediate value for flex
calculation */
Note that as the data definition extends less across the page than executable code, its comment start column can be further to the left. 4.8.2 Comment on valuesWhere the contents of a variable is constrained, either by range or by a limited set of values, it helps the reader to understand its use if some indication of the possible values is included in the comment.
int ParaLen; /* chars in current para <
PARA_LEN_MAX */
The problem with this method is one of maintenance. If the possible values of the variable change, then the comment becomes wrong, and could confuse. 4.8.3 Comment all data declarationsThe decision on which data declarations to comment depends on how 'obvious' the usage and possible values are. To the writer of the code, most things are immediately clear; to the reader, most things are not that obvious. Applying the simplicity principle, the solution is to comment all data declarations:
int StdFlex; /* Intermediate value for flex
calculation */
4.8.4 Comment data tables carefullyEven more important than commenting single data items is commenting data tables. Without explanation, a table is a jumble of numbers, etc. Take the following example:
int AgeFactors[] = { 0, 10, 22, 25, 15, 10, 8, 2 };
What are these 'AgeFactors'? What is the difference between the first and the second elements? Commenting can give the answer. Data tables often require more than half a line of comments, so a block comment is usually a good idea.
/*--------------------------------------------------------------
Note the helpful hints in this comment:
Comments should similarly be applied to other declarations, such as #define's, structure declarations, etc.
struct SAL_REC /* Employee salary details */
|
Site Menu |
Quality: | Quality Toolbook | Tools of the Trade | Improvement Encyclopedia | Quality Articles | Being Creative | Being Persuasive | |
And: | C Style (Book) | Stories | Articles | Bookstore | My Photos | About | Contact | |
Settings: | Computer layout | Mobile layout | Small font | Medium font | Large font | Translate | |
You can buy books here |
And the big |