Ada 95 Quality and Style Guide Chapter 5
5.6.4 Loops
guideline
guideline
guideline
guideline
guideline
guideline
guideline
As noted in Guideline 4.2, Ada's ability to enforce information
hiding and separation of concerns through its visibility controlling
features is one of the most important advantages of the language.
Subverting these features, for example, by too liberal use of
the use clause, is wasteful and dangerous.
5.7.1 The Use Clause
guideline
guideline
guideline
guideline
guideline
Ada exceptions are a reliability-enhancing language feature designed
to help specify program behavior in the presence of errors or
unexpected events. Exceptions are not intended to provide a general
purpose control construct. Further, liberal use of exceptions
should not be considered sufficient for providing full software
fault tolerance (Melliar-Smith and Randell 1987).
This section addresses the issues of how and when to avoid raising
exceptions, how and where to handle them, and whether to propagate
them. Information on how to use exceptions as part of the interface
to a unit includes what exceptions to declare and raise and under
what conditions to raise them. Other issues are addressed in the
guidelines in Sections 4.3 and 7.5.
5.8.1 Handling Versus Avoiding Exceptions
guideline
guideline
guideline
guideline
Ada 95 introduces the category of bounded errors. Bounded errors
are cases where the behavior is not deterministic but falls within
well-defined bounds (Rationale 1995, §1.4). The consequence
of a bounded error is to limit the behavior of compilers so that
an Ada environment is not free to do whatever it wants in the
presence of errors. The Ada Reference Manual (1995) defines a
set of possible outcomes for the consequences of undefined behavior,
as in an uninitialized value or a value outside the range of its
subtype. For example, the executing program may raise the predefined
exception Program_Error, Constraint_Error, or
it may do nothing.
An Ada program is erroneous when it generates an
error that is not required to be detected by the compiler or
>
run-time environments. As stated in the Ada Reference Manual (1995,
§1.1.5), "The effects of erroneous execution are unpredictable."
If the compiler does detect an instance of an erroneous program,
its options are to indicate a compile time error; to insert the
code to raise Program_Error,
possibly to write a message to that effect; or to do nothing at
all.
Erroneousness is not a concept unique to Ada. The guidelines below
describe or explain some specific instances of erroneousness defined
in the Ada Reference Manual (1995). These guidelines are not intended
to be all-inclusive but rather emphasize some commonly overlooked
problem areas. Arbitrary order dependencies are not, strictly
speaking, a case of erroneous execution; thus, they are discussed
in Guideline 7.1.9 as a portability issue.
5.9.1 Unchecked Conversion
guideline
In the case of a nonscalar object used as an actual parameter
in an unchecked conversion, you should ensure that its value on
return from the procedure properly represents a value in the subtype.
This case occurs when the parameter is of mode out or
in out. It is important to check the value when interfacing
to foreign languages or using a language-defined input procedure.
The Ada Reference Manual (1995, §13.9.1) lists the full rules
concerning data validity.
5.9.2 Unchecked Deallocation
guideline
guideline
guideline
guideline
guideline
guideline
guideline
5.9.9 Protected Objects
guideline
5.9.10 Abort Statement
guideline
optional parts of the syntax
example
example
example
rationale
example
example
rationale
example
5.7 VISIBILITY
example
example
rationale
Guideline 3.1.4).
example
rationale
example
rationale
rationale
In redefining equality, you are not required to have a result
type of Standard.Boolean. The Rationale
(1995, §6.3) gives two examples where your result type is
a user-defined type. In a three-valued logic abstraction, you
redefine equality to return one of True, False,
or Unknown. In a vector processing application, you can
define a component-wise equality operator that returns a vector
of Boolean values. In both these instances, you should also redefine
inequality because it is not the Boolean complement of the equality
function.
5.8 USING EXCEPTIONS
rationale
example
rationale
example
5.9 ERRONEOUS EXECUTION AND BOUNDED ERRORS
example
An access value should not be assumed to be correct when obtained
without compiler or run-time checks. When dealing with access
values, use of the 'Valid attribute helps prevent the
erroneous dereferencing that might occur after using Ada.Unchecked_Deallocation,
Unchecked_Access, or Ada.Unchecked_Conversion.
rationale
rationale
example
rationale
rationale
example
rationale
rationale
rationale
Invoking any of these potentially blocking operations could lead
either to a bounded error being detected or to a deadlock situation.
In the case of bounded error, the exception Program_Error
is raised. In addition, avoid calling routines within a protected
entry, procedure, or function that could directly or indirectly
invoke operating system primitives or similar operations that
can cause blocking that is not visible to the Ada run-time system.
rationale
abort-deferred operation.
The Ada Reference Manual (1995, §9.8) states that the practices
discouraged in the guidelines result in bounded error. The exception
Program_Error is raised if the implementation detects
the error. If the implementation does not detect the error, the
operations proceed as they would outside an abort-deferred operation.
An abort statement itself may have no effect.
5.10 SUMMARY
parameter lists
types
data structures
expressions
statements
visibility
using exceptions
erroneous execution and bounded errors
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