The Psychology of Quality and More |
CHAPTER 4 : Commenting
4.5 Function header commentsWhen a reader starts to read the function code, he should already have a very good idea of what it is does, how it does it, and what problems he might meet. The function header comment fulfils this need. 4.5.1 Describe the purposeThe name of the function is probably the most succinct description of its purpose. An additional brief description of the action of the function is usually very helpful in clarifying its use. 4.5.2 Describe the inputs and outputsA function usually processes inputs to create outputs. Describing these inputs and outputs is a clear hint as to what is being done in the function. Inputs include not only parameters, but also global variables and (possibly) constants. Outputs include parameters, global variables and function returns. Describing output values, either explicitly or by reference to where they are defined, helps the user who is following a call to this function. Similarly, describing expectations and limitations on inputs can be helpful. 4.5.3 Describe the processDescribing how the function does what it does, particularly where it uses a complex algorithm or process, can help the user understand the intended action of the code. This section is often used for a complete pseudo-code description of the code below, sometimes with line numbers which are keyed to commented numbers in the code:
Pseudo-code description in function header:
* [2] find employee number * [3] extract and filter out current salary ...
In-line comment used to key to header comment:
for ( EmpNo = 0; EmpNo < NofEmployees; EmpNo++ ) /*[2]*/ ...
..or, copying or moving the entire comment into the code:
/* [2] find employee number */ for ( EmpNo = 0; EmpNo < NofEmployees; EmpNo++ ) ...
The danger of including pseudo-code in the header is that it can easily fall into disrepair (if you fix a bug in the code, will you also fix it in the pseudo-code?). 4.5.3 Describe the external contextDescribing the external context of the function, in terms of the called/calling functions may be desirable, although it can be very difficult to maintain. A good cross-referencing tool is often adequate. 4.5.4 Describe the historical contextEach time the function is changed (after the code is 'frozen'), a note should be included describing the change. This may include columns for the internal defect/fix number, the date the change was made, the name (or initials) of the person making the change, and brief details of the change. The code may be keyed to these notes by using fix reference comments.
Fix note in header:
* FX14326 22Jan99 JR
Fix: backspace at character zero in Matching fix in code:
if ( (CharNo == 0) && (Ch
== BS) ) /*FX14326*/ -------------------------------------------------------------------------> Keeping this information in the code is being very explicit about changes, and may even include old code which is 'commented out' (see 4.9.1). A simpler approach is to extract this when required from the version control system. 4.5.5 Give any other help you canIn a similar manner to file header comments, items that do not easily fit into any of the given categories, such as further warnings, hints, assumptions, limitations, etc. which will help the reader understand the file, can be put into a 'notes' section. 4.5.6 Examples of function header commentUsing the above guidelines, a standard function header may be derived:
/*******************************************************************
A long function header comment will give a lot of information about the function, but it also takes up a lot of room, and requires a significant effort to write. This may be disproportionate where the function that it describes is small, simple, low-level and infrequently read. In this case, many of the fields in the above example may be omitted to allow the size of the header to be scaled down. A minimum header summarizes the inputs, outputs and the basic process. Note the inclusion of the function declaration within the header.
/*F******************************************************************/
|
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 |