Ada 95 Quality and Style Guide Chapter 5
Software is always subject to change. The need for this change,
euphemistically known as "maintenance" arises from a
variety of sources. Errors need to be corrected as they are discovered.
System functionality may need to be enhanced in planned or unplanned
ways. Inevitably, the requirements change over the lifetime of
the system, forcing continual system evolution. Often, these modifications
are conducted long after the software was originally written,
usually by someone other than the original author.
Easy and successful modification requires that the software be
readable, understandable, and structured according to accepted
practice. If a software component cannot be easily understood
by a programmer who is familiar with its intended function, that
software component is not maintainable. Techniques that make code
readable and comprehensible enhance its maintainability. Previous
chapters presented techniques such as consistent use of naming
conventions, clear and well-organized commentary, and proper modularization.
This chapter presents consistent and logical use of language features.
Correctness is one aspect of reliability. While style guidelines
cannot enforce the use of correct algorithms, they can suggest
the use of techniques and language features known to reduce the
number or likelihood of failures. Such techniques include program
construction methods that reduce the likelihood of errors or that
improve program predictability by defining behavior in the presence
of errors.
Parts of the Ada syntax, while optional, can enhance the readability
of the code. The guidelines given below concern use of some of
these optional features.
5.1.1 Loop Names
guideline
guideline
rationale
guideline
guideline
A subprogram or entry parameter list is the interface to the abstraction
implemented by the subprogram or entry. It is important that it
is clear and that it is expressed in a consistent style. Careful
decisions about formal parameter naming and ordering can make
the purpose of the subprogram easier to understand, which can
make it easier to use.
5.2.1 Formal Parameters
guideline
rationale
guideline
rationale
guideline
guideline
In addition to determining the possible values for variables and
subtype names, type distinctions can be very valuable aids in
developing safe, readable, and understandable code. Types clarify
the structure of your data and can limit or restrict the operations
performed on that data. "Keeping types distinct has been
found to be a very powerful means of detecting logical mistakes
when a program is written and to give valuable assistance whenever
the program is being subsequently maintained" (Pyle 1985).
Take advantage of Ada's strong typing capability in the form of
subtypes, derived types, task types, protected types, private
types, and limited private types.
The guidelines encourage much code to be written to ensure strong
typing. While it might appear that there would be execution penalties
for this amount of code, this is usually not the case. In contrast
to other conventional languages, Ada has a less direct relationship
between the amount of code that is written and the size of the
resulting executable program. Most of the strong type checking
is performed at compilation time rather than execution time, so
the size of the executable code is not greatly affected.
For guidelines on specific kinds of data structures and tagged
types, see Guidelines 5.4 and 9.2.1, respectively.
5.3.1 Derived Types and Subtypes
guideline
guideline
guideline
guideline
The data structuring capabilities of Ada are a powerful resource;
therefore, use them to model the data as closely as possible.
It is possible to group logically related data and let the language
control the abstraction and operations
on the data rather than requiring the programmer or maintainer
to do so. Data can also be organized in a building block fashion.
In addition to showing how a data structure is organized (and
possibly giving the reader an indication as to why it was organized
that way), creating the data structure from smaller components
allows those components to be reused. Using the features that
Ada provides can increase the maintainability of your code.
5.4.1 Discriminated Records
guideline
rationale
guideline
rationale
guideline
rationale
guideline
5.4.5 Dynamic Data
guideline
guideline
rationale
guideline
guideline
rationale
Properly coded expressions can enhance the readability and understandability
of a program. Poorly coded expressions can turn a program into
a maintainer's nightmare.
5.5.1 Range Values
guideline
5.5.2 Array Attributes
guideline
guideline
guideline
guideline
guideline
Careless or convoluted use of statements can make a program hard
to read and maintain even if its global structure is well organized.
You should strive for simple and consistent use of statements
to achieve clarity of local program structure. Some of the guidelines
in this section counsel use or avoidance of particular statements.
As pointed out in the individual guidelines, rigid adherence to
those guidelines would be excessive, but experience has shown
that they generally lead to code with improved reliability and
maintainability.
5.6.1 Nesting
guideline
guideline
guideline
5.1 OPTIONAL PARTS OF THE SYNTAX
example
rationale
example
example
example
rationale
5.2 PARAMETER LISTS
example
instantiation
example
example
example
rationale
5.3 TYPES
example
example
example
rationale
example
5.4 DATA STRUCTURES
example
example
example
example
rationale
example
example
example
example
5.5 EXPRESSIONS
example
rationale
example
rationale
example
rationale
example
example
example
rationale
5.6 STATEMENTS
instantiation
exceptions
example
rationale
example
rationale
Chapter 5 continued on the 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