Ada Coding Standard Ada Coding Standard: BSSC (98) 3 Issue 1 October 1998
Ada Coding Standard Ada Coding Standard: BSSC (98) 3 Issue 1 October 1998
October 1998
Prepared by:
ESA Board for Software
Standardisation and Control
(BSSC)
1 0 1998
TABLE OF CONTENTS
PREFACE................................................................................................................. V
INTRODUCTION.......................................................................................................1
1.1 PURPOSE.......................................................................................................1
1.2 OVERVIEW .....................................................................................................1
CHAPTER 4 ........................................................................................................... 14
PROGRAMMING PRACTICES.............................................................................. 20
5.1 OPTIONAL PARTS OF THE SYNTAX ...........................................................20
5.2 PARAMETER LISTS......................................................................................20
5.3 TYPES ..........................................................................................................21
5.4 DATA STRUCTURES....................................................................................22
5.5 EXPRESSIONS .............................................................................................24
5.6 STATEMENTS ..............................................................................................25
5.7 GENERICS ...................................................................................................27
5.8 EXCEPTIONS ...............................................................................................28
5.9 CONCURRENCY..........................................................................................30
iv BSSC(98)3 Issue 1
TABLE OF CONTENTS
5.9.1Tasking 31
5.9.2Priorities ....................................................................................................31
5.9.3Communication.........................................................................................32
5.9.4 Termination............................................................................................33
DEPENDENCIES ................................................................................................... 35
6.1 INTRODUCTION ..........................................................................................35
6.2 FUNDAMENTALS.........................................................................................35
6.3 NUMERIC TYPES AND EXPRESSIONS .......................................................36
6.4 REPRESENTATION CLAUSES .....................................................................38
6.5 IMPLEMENTATION-DEPENDENT FEATURES .............................................39
6.6 PRAGMAS....................................................................................................39
APPENDIX B REFERENCES................................................................................B-1
BSSC(98)3 Issue 1 v
PREFACE
PREFACE
The BSSC wishes to thank Tullio Vardanega and Richard Jack for preparing
the standard. The authors wish to thank all the people who contributed ideas for this
standard.
CHAPTER 1
INTRODUCTION
1.1 PURPOSE
1.2 OVERVIEW
CHAPTER 2
THE ADA PROGRAMMING LANGUAGE
2.1 INTRODUCTION
The Ada programming language, which was first defined as
ANSI/MIL-STD-1815A, has been specified as an ISO (International
Standardisation Organisation) standard since 1987 [Ref. 1]. This
language standard has since been the subject of an extensive revision
process which has led to [Ref. 2].
The language standard in its former version is commonly known
as Ada 83 whereas the language standard in its latest version is referred
to as Ada 95.
The ISO body in charge of the language revision has defined the
intended transition process from Ada 83 to Ada 95. The transition
process foresees a limited period of time (1995-1998) during which both
language versions would coexist and both conformance certification
suites (known as ACVCs) would be maintained.
At the end of the transition period, the old Ada standard shall be
declared obsolete and all new certificates of conformance shall solely
address the revised Ada standard.
An ISO working group to which ESA is actively participating has
prepared a guidance document to the production of High-Integrity Ada
Systems [Ref. 3]. This guidance document should constitute one major
complement to the present coding standard.
organisation and contents of the AJPO’s Ada Quality and Style document
[Ref. 5] which is presently recognised as the single most authoritative
source of coding guidelines for Ada programmers.
CHAPTER 3
SOURCE CODE PRESENTATION
• horizontal spacing
• line length.
The practices are defined in the table below. In this and
subsequent tables of practices, numbered supporting examples are
found in Appendix A.
Id Practice Supporting
Example
Id Practice Supporting
Example
004 Indentation and alignment of continuation lines
using a two-space basic step of indentation shall
be used.
Id Practice Supporting
Example
Id Practice Supporting
Example
Id Requirement Supporting
Example
Id Requirement Supporting
Example
evaluated at compile time with full precision.
Hence, named numbers should be used as the
target of all the static numeric expressions in the
program.
3.4 Comments
Id Practice Supporting
example
• file headers
• data comments
• statement comments
• marker comments.
Id Practice Supporting
example
Id Practice Supporting
example
supplied, unless their names are totally self-
explanatory and their usage obvious.
CHAPTER 4
PROGRAM STRUCTURE
4.1 INTRODUCTION
Id Practice Supporting
Example
Id Practice Supporting
Example
separation. denotes a file
containing a
library unit
package
specification
whereas the
suffix .adb
denotes its
body)
• information hiding
• encapsulation of implementation
dependencies
Id Practice Supporting
Example
decisions and program details.
4.3 VISIBILITY
Id Practice Supporting
example
Id Practice Supporting
example
subprogram shall be avoided.
Id Practice Supporting
example
relevant algebra (e.g.: “+” must still mean
adding, even under overloading).
Id Practice Supporting
example
070 The number of ways to exit a loop shall be
minimised.
071 The use of: exit when <condition> shall be
preferred to the use of:
if <condition> then exit.
CHAPTER 5
PROGRAMMING PRACTICES
Id Practice Supporting
example
Id Practice Supporting
example
5.3 TYPES
• predefined and new types primarily serve as building blocks for the
creation of more specialised type derivatives
• derived types represent new types to all effect and are, therefore,
different from their base type
• subtypes limit the range of possible values for the base type but do
not define new types.
Id Practice Supporting
example
088 Heterogeneous but related data shall be grouped Example 11
using records.
089 Discriminated records capture the intent of arrays
whose bounds may vary at run-time. Arrays with
such characteristics shall be modelled using
discriminated records rather than constrained
arrays.
090 Discriminants shall be based on as constrained a Example 12
subtype as possible. This reduces the amount of
useless (i.e.: wasted) space allocated to the
object.
091 The use of large flat record structures shall be Example 13
avoided; wherever possible, related components
shall be grouped into smaller subrecords.
Complex data structures are better
understandable if composed of meaningfully
related building blocks that have been factored
out.
092 The use of dynamically allocated objects shall be
carefully considered, preferably avoided, and in
all cases justified.
Note: Dynamic allocation (performed by execution
of the allocator new) and deallocation of objects
(performed by execution of
Unchecked_Deallocation or implicitly caused by
the object’s access type going out of scope)
involve heap management at run-time.
Heap operations are intrinsically unbounded and
potentially unpredictable; they are, therefore,
antagonistic to the construction of high-integrity,
predictable systems.
093 If the use of dynamically allocated data structures
is required, specific measures shall be enforced to
ensure, at least, that:
24 BSSC(98)3 Issue 1
PROGRAMMING PRACTICES
Id Practice Supporting
example
• no dangling references are left to deallocated
objects
• pointers to undeallocated objects are not
dropped
• all deallocations are performed explicitly
• all access variables are initialised explicitly
• handlers are provided for Storage_Error
exceptions
• very precise information is provided as to
whether and how the run-time system reclaims
heap storage.
094 The use of uninitialised objects is a breach to
program’s integrity.
All variable objects shall, therefore, be initialised
prior to use.
This is best achieved by assigning objects an
initial value at the time of declaration.
095 Entities in an Ada program need to be elaborated Example 14
before being ready for use.
Care shall be taken to ensure the elaboration of an
entity before using it.
5.5 EXPRESSIONS
Properly coded expressions enhance the readability and
understandability of a program.
The practices are defined in the table below.
Id Practice Supporting
example
096 The use of constructs and expressions whose
meaning or effect depends on the order of
evaluation shall be avoided.
BSSC(98)3 Issue 1 25
PROGRAMMING PRACTICES
Id Practice Supporting
example
097 Parentheses shall be used to specify and clarify Example 15
the desired order of evaluation in expressions.
5.6 STATEMENTS
Careless or convoluted use of statements can make a program
difficult to read and maintain even if its global structure is well organised.
The practices are defined in the table below.
Id Practices Supporting
example
101 The depth of nested expressions shall be
26 BSSC(98)3 Issue 1
PROGRAMMING PRACTICES
Id Practices Supporting
example
minimised.
102 The depth of nested control structures shall be
minimised.
103 Slices should be used to copy part of an array.
104 Aggregates shall be used instead of a sequence Example 18.
of assignments to assign values to all components
of a record.
105 The use of others choice is case statements shall
be minimised.
106 Ranges of enumeration literals shall not be used in
case statements.
Id Practices Supporting
example
comments should clarify: all of the pre-requisites
and assumptions which hold on the expected
contents of the source data (e.g.: range, meaning
and authentic interpretation); the rationale for the
choice of the destination type; and the
precautionary measures taken to avoid the
occurrence of constraint violations.
5.7 GENERICS
The use of generic units, however, is not the only way to promote
reuse in Ada. Factorisation of data structures and related operations is a
more natural and effective means to support reuse of building blocks
across one program or programs within the same application domain.
5.8 EXCEPTIONS
Ada exceptions are language features designed to help one
specify the intended program behaviour in the face of errors or
unexpected events. The use of exceptions, however, is provably not
sufficient to provide for the comprehensive implementation of software
fault tolerance.
The practices are defined in the table below.
Id Practices Supporting
example
113 Exceptions shall not be used as a general-
purpose control flow modifier.
114 Care shall be taken to avoid deliberate or implicit Examples of
raising of exceptions. defensive
The use of defensive programming is a suitable programming
means to do so. techniques
are:
• assertive
verification
of an
object’s
correct
value prior
to use
• localisation
of
potentially
exception-
raising
operations
BSSC(98)3 Issue 1 29
PROGRAMMING PRACTICES
Id Practices Supporting
example
within
enclosing
blocks
provided
with
exception
handlers.
115 Handlers shall be provided for all the exceptions
that cannot be avoided and all that are explicitly
allowed.
Note: When execution performance is an issue, it
may be more efficient to perform potentially
exception-raising operations within the scope of
an exception handler than to exclusively rely on
defensive programming.
116 Care shall be taken to avoid undesired
propagation of exceptions.
Note: Predefined and implementation-defined
exceptions are natural candidates for
propagation to higher levels of abstractions; it
may, in fact, prove difficult to determine exactly
which statement and which operation within that
statement raised an exception.
User-defined exceptions are more easily
associated with useful recovery actions by
localising handlers within small blocks of code
(e.g.: a subprogram body).
117 The use of others exceptions shall be avoided
or just limited to development for the purpose of
capturing, qualifying and providing specific
handlers for potential exceptions.
118 Exception checks is a powerful debugging aid
during development.
Exception checks shall not be suppressed
30 BSSC(98)3 Issue 1
PROGRAMMING PRACTICES
Id Practices Supporting
example
during development.
Note: Removal of exception checks during
operation is a very questionable practice. With
exception checks disabled, program execution
which results in a condition where an exception
would otherwise occur becomes erroneous and
its results are unpredictable.
119 Disabling of exception checks during operation
shall be duly justified and its possible
consequences carefully assessed.
5.9 CONCURRENCY
5.9.1 Tasking
5.9.2 Priorities
5.9.3 Communication
Ada provides a rich set of constructs to model various forms of
synchronisation between co-operating tasks. Not all such forms of
synchronisation equally suit differing application domains: time-critical
applications, for example, will probably need to define tight restrictions
on the allowable type of synchronisation.
The practices are defined in the table below.
Id Practice Supporting
example
128 Care shall be taken to establish the set of
synchronisation constructs appropriate for the
specific application.
129 Recommendations for the use of synchronisation
constructs shall be provided as part of any
suitable programming model which allows the use
of co-operating tasks.
130 Entries of a task shall not directly or indirectly call
entries of the original calling task because this
BSSC(98)3 Issue 1 33
PROGRAMMING PRACTICES
Id Practice Supporting
example
determines deadlock situations.
131 The work performed during rendez-vous shall be
minimised as it blocks both the caller and the
callee.
132 Careful consideration shall be given to the
opportunity of catering for exception handling
during rendez-vous.
133 The work performed in the selective accept loop
of a server task shall be minimised as it increases
the time it takes to return to the next accept, in
addition to being prone to potential suspension of
the server.
134 Shared variables shall not be used as a task
synchronisation device: guards shall only
reference local variables.
135 The number of accept and select statements
within a task shall be minimised.
136 The number of accept statements per entry shall
be minimised. (Ideally, there shall be a one-to-one
correspondence between an entry and its
accept statement.)
5.9.4 Termination
The definition of the conditions under which tasks may need to
terminate is an important design decision.
Very few guidelines regarding task termination can be given
which have a sufficiently general applicability.
The practices are defined in the table below.
Id Practice Supporting
example
137 The use of an exception handler for a rendez-vous
34 BSSC(98)3 Issue 1
PROGRAMMING PRACTICES
CHAPTER 6
DEPENDENCIES
6.1 INTRODUCTION
6.2 FUNDAMENTALS
Four general principles can be laid down which help one lessen
the effect of dependency on implementation-sensitive Ada constructs.
Such principles are the following:
• localise and encapsulate (by use of the Ada’s structuring facilities) the
non-portable features of a program whose use is essential
Id Practice Supporting
example
• number of bits available for type Integer
• number of decimal digits of precision available
for floating-point types
• number of bits available for fixed-point types
• number of characters per line of source text.
142 Highlighting comments shall be used to mark the
use of non-portable features.
143 Packages shall be specifically created to isolate
and encapsulate hardware and implementation
dependencies.
144 Packages encapsulating dependencies shall be
designed so as to resolve dependencies within
target-specific bodies thereby allowing
specifications not to change when porting.
145 Code whose correct execution depends on
implementation-specific properties (e.g.:
parameter passing mechanisms) shall be
avoided.
Id Practice Supporting
example
application.
Note: Effective use of floating-point or fixed-point
operations requires the determination of the
accuracy provided by the specific
implementation of the language.
147 Reliance on general-purpose predefined numeric
types should be avoided. The language typing
facility should be used to create the numeric
types appropriate to the problem.
Note: Floating-point operations are often directly
supported by the hardware of modern platforms
and the resulting precision is relatively
straightforward to determine for any given
implementation. Within the capability of the
hardware, clause digits determines the
accuracy required of the associated floating-
point type.
The Ada fixed point, instead, requires more
extensive compiler support and, hence, it is more
prone to diversities between implementations.
The required precision of the fixed-point type is
set by use of clause delta. Simple means exist,
which help one detect peculiarities of the fixed-
point implementation in any given language
implementation (c.f., for example, [Ref. 7].)
148 Reliance on the default accuracy of fixed-point or
floating-point types shall be avoided. The
language facility (digits, delta) shall be used
to specify the required precision of the type.
Note: Further control on the precision of fixed-
point and floating-point types can be obtained by
verification of the type’s attributes.
149 Avoid modifying the default value of the fixed-
point and floating-point type’s attributes. Some of
these attributes, in fact, play a key role in the
38 BSSC(98)3 Issue 1
DEPENDENCIES
Id Practice Supporting
example
accuracy of the operations on the type (e.g.:
small).
6.6 PRAGMAS
This standard takes the view that the use of pragma clauses is a
project-specifc practice. Conformance with ad-hoc project-specific
guidelines in this respect, therefore, takes precedence over any generic
standard.
Id Practice Supporting
example
to support additional pragma clauses.
158 Reliance shall be placed on project-specific
guidelines for the use of the implementation-defined
pragma clauses.
159 Highlighting comments shall be used to mark the use
and intent of each pragma in the program.
160 Highlighting comments shall be used to flag every
use of implementation-defined pragma clauses in the
program.
BSSC(98)3 Issue 1 41
DEPENDENCIES
• boldfaced font is used for Ada reserved words when used in plain
text
. end if;
.
Example 4 : Use of blank lines for grouping
. type Employee_Record is
. record
. Legal_Name : Name;
. Date_Of_Birth : Date;
. Date_Of_Hire : Date;
. Position : Grade;
. end record;
.
. type Day is
. ( Monday, Tuesday, Wednesday, Thursday, Friday,
. Saturday, Sunday );
.
. subtype Weekday is Day range Monday .. Friday;
. subtype Weekend is Day range Saturday .. Sunday;
Example 12 : Discriminants
.
. Max_Length : constant := 42; -- list contains at most 42 items
. -- but objects of type Item_Holder
. -- may have smaller lists
.
. type Item_List is array ( Integer range <> ) of Integer;
. subtype Max_Items is Integer range 1 .. Max_Length;
.
. type Item_Holder ( Current_Length : Max_Items := 1 ) is
. record
. Items : Item_List ( 1 .. Current_Length );
. end record;
.
. end record;
.
. type Bounding_Box is
. record
. Top_Left : Coordinate;
. Bottom_Right : Coordinate;
. end record;
.
APPENDIX B REFERENCES