Ada 95 Quality and Style Guide Chapter 2
The physical layout of source text on the page
or screen has a strong effect on its readability. This chapter
contains source code presentation guidelines intended to make
the code more readable.
In addition to the general purpose guidelines, specific recommendations
are made in the "instantiation" sections. If you disagree
with the specific recommendations, you may want to adopt your
own set of conventions that still follow the general purpose guidelines.
Above all, be consistent across your entire project.
An entirely consistent layout is hard to achieve or check manually.
Therefore, you may prefer to automate layout with a tool
for parameterized code formatting or incorporate the guidelines
into an automatic coding template. Some of the guidelines and
specific recommendations presented in this chapter cannot be enforced
by a formatting tool because they are based on the
semantics, not the syntax, of the Ada code. More details are given
in the "automation notes" sections.
The "code formatting" of Ada source code affects how
the code looks, not what the code does. Topics included here are
horizontal spacing, indentation, alignment, pagination, and line
length. The most important guideline is to be consistent throughout
the compilation unit as well as the project.
2.1.1 Horizontal Spacing
guideline
guideline
A label is outdented three spaces:
The if statement and the plain loop:
Loops with the for and while
iteration schemes:
The block and the case
statement as recommended in the Ada Reference Manual (1995):
These case statements save space
over the Ada Reference Manual (1995) recommendation and depend
on very short statement lists, respectively. Whichever you choose,
be consistent:
The various forms of selective
accept and the timed and conditional
entry calls:
The accept statement:
A subunit:
Proper bodies of program
units:
Context
clauses on compilation units are arranged as a table. Generic
formal parameters do not obscure the unit itself. Function,
package, and task
specifications use standard indentation:
Instantiations of generic units
and record indentation:
Indentation for record
alignment:
Tagged types and type extension:
guideline
guideline
guideline
guideline
guideline
guideline
guideline
code formatting
2.1 CODE FORMATTING
instantiation
+ - * / &
< = > /= <= >=
:= => | ..
:
<>
example
rationale
exceptions
automation notes
2.1.2 Indentation
instantiation
example
rationale
exceptions
automation notes
2.1.3 Alignment of Operators
begin
<<label>> | <long statement with line break>
<statement> | <trailing part of same statement>
end;
if <condition> then | <name>:
<statements> | loop
elsif <condition> then | <statements>
<statements> | exit when <condition>;
else | <statements>
<statements> | end loop <name>;
end if; |
<name>: | <name>:
for <scheme> loop | while <condition> loop
<statements> | <statements>
end loop <name>; | end loop <name>;
<name>: | case <expression> is
declare | when <choice> =>
<declarations> | <statements>
begin | when <choice> =>
<statements> | <statements>
exception | when others =>
when <choice> => | <statements>
<statements> | end case; --<comment>
when others => |
<statements> |
end <name>; |
case <expression> is | case <expression> is
when <choice> => | when <choice> => <statements>
<statements> | <statements>
when <choice> => | when <choice> => <statements>
<statements> | when others => <statements>
when others => | end case;
<statements> |
end case; |
select | select
when <guard> => | <entry call>;
<accept statement> | <statements>
<statements> | or
or | delay <interval>;
<accept statement> | <statements>
<statements> | end select;
or |
when <guard> => | select
delay <interval>; | <entry call>;
<statements> | <statements>
or | else
when <guard> => | <statements>
terminate; | end select;
else |
<statements> | select
end select; | <triggering alternative>
| then abort
| <abortable part>
| end select;
accept <specification> do | separate (<parent unit>)
<statements> | <proper body>
end <name>; |
separate (<parent unit>)
<proper body>
end <name>;
procedure <specification> is | package body <name> is
<declarations> | <declarations>
begin | begin
<statements> | <statements>
exception | exception
when <choice> => | when <choice> =>
<statements> | <statements>
end <name>; | end <name>;
|
function <specification> | task body <name> is
return <type name> is | <declarations>
<declarations> | begin
begin | <statements>
<statements> | exception
exception | when <choice> =>
when <choice> => | <statements>
<statements> | end <name>;
end <name>; |
with <name>; use <name>; | function <specification>
with <name>; | return <type>;
with <name>; |
| package <name> is
| <declarations>
| private
<compilation unit> | <declarations>
| end <name>;
|
generic | task type <name> is
<formal parameters> | <entry declarations>
<compilation unit> | end <name>;
procedure <name> is |type ... is
new <generic name> <actuals> | record
| <component list>
function <name> is | case <discriminant name> is
new <generic name> <actuals> | when <choice> =>
| <component list>
package <name> is | when <choice> =>
new <generic name> <actuals> | <component list>
| end case;
| end record;
for <name> use
record <mod clause>
<component clause>
end record;
type ... is tagged
record
<component list>
end record;
type ... is new ... with
record
<component list>
end record;
example
rationale
exceptions
automation notes
2.1.4 Alignment of Declarations
instantiation
example
rationale
automation notes
2.1.5 More on Alignment
instantiation
example
rationale
notes
exceptions
automation notes
2.1.6 Blank Lines
example
rationale
automation notes
2.1.7 Pagination
instantiation
example
rationale
notes
automation notes
2.1.8 Number of Statements Per Line
example
rationale
notes
automation notes
exceptions
2.1.9 Source Code Line Length
instantiation
rationale
notes
automation notes
2.2 SUMMARY
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