Ada 95 Quality and Style Guide Chapter 3
This chapter recommends ways of using Ada features to make reading
and understanding code easier. There are many myths about comments
and readability. The responsibility for true readability rests
more with naming and code structure than with comments. Having
as many comment lines as code lines does not imply readability;
it more likely indicates the writer does not understand what is
important to communicate.
Spelling conventions in source code include rules for capitalization
and use of underscores, numbers, and abbreviations. If you follow
these conventions consistently, the resulting code is clearer
and more readable.
3.1.1 Use of Underscores
guideline
guideline
guideline
guideline
rationale
Choose names that clarify the object's or entity's intended use.
Ada allows identifiers to be any length as long as the identifier
fits on a line with all characters being significant (including
underscores). Identifiers
are the names used for variables, constants, program units, and
other entities within a program.
3.2.1 Names
guideline
guideline
guideline
guideline
guideline
guideline
guideline
guideline
Comments in source text are a controversial issue. There are arguments
both for and against the view that comments enhance readability.
In practice, the biggest problem with comments is that people
often fail to update them when the associated source text is changed,
thereby making the commentary misleading. Commentary should be
reserved for expressing needed information that cannot be expressed
in code and highlighting cases where there are overriding reasons
to violate one of the guidelines. If possible, source text should
use
Use comments to state the intent of the code. Comments that provide
an overview of the code help the maintenance programmer see the
forest for the trees. The code itself is the detailed "how"
and should not be paraphrased in the comments.
Comments should be minimized. They should
provide needed information that cannot be expressed in the Ada
language, emphasize the structure of code, and draw attention
to deliberate and necessary violations
of the guidelines. Comments are present either to draw attention
to the real issue being exemplified or to compensate for incompleteness
in the sample program.
Maintenance programmers need to know the causal interaction of
noncontiguous pieces of code to get a global, more or less complete
sense of the program. They typically acquire this kind of information
from mental simulation of parts of the code. Comments should be
sufficient enough to support this process (Soloway et al. 1986).
This section presents general guidelines about how to write good
comments. It then defines several different classes of comments
with guidelines for the use of each. The classes are file headers,
program unit specification headers, program unit body headers,
data comments, statement comments, and marker comments.
3.3.1 General Comments
guideline
guideline
rationale
3.1 SPELLING
example
rationale
3.1.2 Numbers
instantiation
example
rationale
notes
3.1.3 Capitalization
instantiation
example
rationale
automation notes
3.1.4 Abbreviations
example
3.2 NAMING CONVENTIONS
example
rationale
notes
3.2.2 Subtype Names
example
rationale
notes
3.2.3 Object Names
example
rationale
notes
3.2.4 Naming of Tagged Types and Associated Packages
instantiation
example
rationale
notes
3.2.5 Program Unit Names
example
rationale
notes
3.2.6 Constants and Named Numbers
example
rationale
notes
3.2.7 Exceptions
example
rationale
3.2.8 Constructors
instantiation
example
rationale
3.3 COMMENTS
self-explanatory names for objects and program units, and it should
use simple, understandable program structures so that little additional
commentary is needed. The extra effort in selecting (and entering)
appropriate names and the extra thought needed to design clean
and understandable program structures are fully justified.
rationale
automation notes
3.3.2 File Headers
instantiation
example
Chapter 3 Continued Next Page
In This Guide:
Table of Contents
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Chapter 10
Chapter 11
Appendix
References
Bibliography
Index