Structure comments in header so that information can be automatically extracted by a tool.
rationale
The structure and function of well-written code is clear without
comments. Obscured or badly structured code is hard to understand,
maintain, or reuse regardless of comments. Bad code should
be improved, not explained. Reading the code itself is the
only way to be absolutely positive about what the code does; therefore,
the code should be made as readable as possible.
Using comments to duplicate information in the code is a bad idea
for several reasons. First, it is unnecessary work that decreases
productivity. Second, it is very difficult to correctly maintain
the duplication as the code is modified. When changes are made
to existing code, it is compiled and tested to make sure that
it is once again correct. However, there is no automatic mechanism
to make sure that the comments are correctly updated to reflect
the changes. Very often, the duplicate information in a comment
becomes obsolete at the first code change and remains so through
the life of the software. Third, when comments about an entire
system are written from the limited point of view of the author
of a single subsystem, the comments are often incorrect from the
start.
Comments are necessary to reveal information difficult or impossible
to obtain from the code. Subsequent chapters of this book contain
examples of such comments. Completely and concisely present the
required information.
The purpose of comments is to help readers understand the code.
Misspelled, ungrammatical, ambiguous, or incomplete comments defeat
this purpose. If a comment is worth adding, it is worth adding
correctly in order to increase its usefulness.
Making comments visually distinct from the code by indenting them,
grouping them together into headers, or highlighting them with
dashed lines is useful because it makes the code easier to read.
Subsequent chapters of this book elaborate on this point.
automation notes
The guideline about storing redundant information in comments
applies only to manually generated comments. There are tools that
automatically maintain information about the code (e.g., calling
units, called units, cross-reference information, revision histories,
etc.), storing it in comments in the same file as the code. Other
tools read comments but do not update them, using the
information from the comments to automatically generate detailed
design documents and other reports.
The use of such tools is encouraged and may require that you structure
your header comments so they can be automatically extracted and/or
updated. Beware that tools that modify the comments in a file
are only useful if they are executed frequently enough. Automatically
generated obsolete information is even more dangerous than manually
generated obsolete information because it is more trusted by the
reader.
Revision histories are maintained much more accurately and completely
by configuration management tools. With no tool support, it is
very common for an engineer to make a change and forget to update
the revision history. If your configuration management tool is
capable of maintaining revision histories as comments in the source
file, then take advantage of that capability, regardless of any
compromise you might have to make about the format or location
of the revision history. It is better to have a complete revision
history appended to the end of the file than to have a partial
one formatted nicely and embedded in the file header.
3.3.2 File Headers
guideline
Put a file header on each source file.
Place ownership, responsibility, and history information for
the file in the file header.
instantiation
- Put a copyright notice in the file header.
- Put the author's name and department in the file header.
- Put a revision history in the file header, including a summary
of each change, the date, and the name of the person making the
change.
example
------------------------------------------------------------------------
-- Copyright (c) 1991, Software Productivity Consortium, Inc.
-- All rights reserved.
-- Author: J. Smith
-- Department:System Software Department
-- Revision History:
-- 7/9/91 J. Smith
-- - Added function Size_Of to support queries of node sizes.
-- - Fixed bug in Set_Size which caused overlap of large nodes.
-- 7/1/91 M. Jones
-- - Optimized clipping algorithm for speed.
-- 6/25/91 J. Smith
-- - Original version.
------------------------------------------------------------------------
rationale
Ownership information should be present in each file if you want
to be sure to protect your rights to the software. Furthermore,
for high visibility, it should be the first thing in the file.
Responsibility and revision history information should be present
in each file for the sake of future maintainers; this is the header
information most trusted by maintainers because it accumulates.
It does not evolve. There is no need to ever go back and modify
the author's name or the revision history of a file. As the code
evolves, the revision history should be updated to reflect each
change. At worst, it will be incomplete; it should rarely be wrong.
Also, the number and frequency of changes and the number of different
people who made the changes over the history of a unit can be
good indicators of the integrity of the implementation with respect
to the design.
Information about how to find the original author should be included
in the file header, in addition to the author's name, to make
it easier for maintainers to find the author in case questions
arise. However, detailed information like phone numbers, mail
stops, office numbers, and computer account user names are too
volatile to be very useful. It is better to record the department
for which the author was working when the code was written. This
information is still useful if the author moves offices, changes
departments, or even leaves the company because the department
is likely to retain responsibility for the original version of
the code.
notes
With modern configuration management systems, explicitly capturing
version history as header comments may be superfluous. The configuration
management tool maintains a more reliable and consistent (from
a content point of view) change history. Some systems can re-create
earlier versions of a unit.