Section 15.1 - Ada Program Structure
Because this is a tutorial I've used very short programs and program
fragments to demonstrate specific points.
Real Ada programs are developed instead as a set of (many) Ada packages.
Each package has a declaration that publicly declares the facilities
it makes available, and each package has a body to implement that public
declaration.
Packages are the principle structuring facility in Ada.
Packages are intended to be used to directly support abstraction,
information hiding, and modularization.
For example, packages are useful for encapsulating machine dependencies.
It isn't easy to describe program structuring issues, because that's
really a program design question, not just a language question.
Some recommendations are given in
Ada Quality and Style: Guidelines for Professional Programmers,
particularly in
Chapter 4 ("Program Structure").
Here are some of those guidelines:
-
Packages should serve a single purpose.
You should avoid creating packages that are
simply collections of unrelated objects and subprograms.
-
You should use a package to group together closely related types.
For example, you might have a
"vector" type (a one-dimensional list of numbers)
and "matrix" type (a two-dimensional list of numbers).
In this case, you could have a single package
called Matrix_Manipulation with both types defined in it.
-
Put only what is needed for the use of a package into its
specification; hide the implementation details from the users.
-
In general,
avoid defining a variable in a package specification, since that
creates a global variable visible to anyone who withs the package.
-
Minimize the number of declarations in a package specification.
-
Only use with clauses where they are needed.
In particular, if you can move a with clause so that it's used by
the package body instead of the package specification,
you should do so.
-
Don't use "use" clauses too liberally.
Note that Ada is different from some other languages.
Ada separates the concept of type from
the concept of module (package); some other languages
merge the two concepts (particularly Eiffel, and to a lessor degree C++).
Each approach has its advantages and disadvantages, which we
won't delve into here.
Quiz:
Which of the following statements is false?
- Hide the implementation details from the users -
Put only what is needed for the use of a package into its specification.
- You should avoid creating packages that are
simply collections of unrelated objects and subprograms.
- Packages are rarely used in real Ada programs.
- Statement 1.
- Statement 2.
- Statement 3.
You may also:
David A. Wheeler (dwheeler@dwheeler.com)
The master copy of this file is at
"http://www.adahome.com/Tutorials/Lovelace/s15s1.htm".