Ada 95 Quality and Style Guide | Chapter 5 |

5.7.1 The Use Clause

guideline

- When you need to provide visibility to operators, use the
use typeclause.

Avoid/minimize the use of the useclause (Nissen and Wallis 1984).

Consider using a package renamesclause rather than auseclause for a package.

Consider using the useclause in the following situations:

- - When standard packages are needed and no ambiguous references are introduced

- - When references to enumeration literals are needed
Localize the effect of all useclauses.

exampleThis is a modification of the example from Guideline 4.2.3. The effect of a

useclause is localized:---------------------------------------------------------------------------------- package Rational_Numbers is type Rational is private; function "=" (X, Y : Rational) return Boolean; function "/" (X, Y : Integer) return Rational; -- construct a rational number function "+" (X, Y : Rational) return Rational; function "-" (X, Y : Rational) return Rational; function "*" (X, Y : Rational) return Rational; function "/" (X, Y : Rational) return Rational; -- rational division private ... end Rational_Numbers; ---------------------------------------------------------------------------------- package body Rational_Numbers is procedure Reduce (R : in out Rational) is . . . end Reduce; . . . end Rational_Numbers; ---------------------------------------------------------------------------------- package Rational_Numbers.IO is ... procedure Put (R : in Rational); procedure Get (R : out Rational); end Rational_Numbers.IO; ---------------------------------------------------------------------------------- with Rational_Numbers; with Rational_Numbers.IO; with Ada.Text_IO; procedure Demo_Rationals is package R_IO renames Rational_Numbers.IO; use type Rational_Numbers.Rational; use R_IO; use Ada.Text_IO; X : Rational_Numbers.Rational; Y : Rational_Numbers.Rational; begin -- Demo_Rationals Put ("Please input two rational numbers: "); Get (X); Skip_Line; Get (Y); Skip_Line; Put ("X / Y = "); Put (X / Y); New_Line; Put ("X * Y = "); Put (X * Y); New_Line; Put ("X + Y = "); Put (X + Y); New_Line; Put ("X - Y = "); Put (X - Y); New_Line; end Demo_Rationals;

rationaleThese guidelines allow you to maintain a careful balance between maintainability and readability. Use of the

useclause may indeed make the code read more like prose text. However, the maintainer may also need to resolve references and identify ambiguous operations. In the absence of tools to resolve these references and identify the impact of changing use clauses, fully qualified names are the best alternative.Avoiding the

useclause forces you to use fully qualified names. In large systems, there may be many library units named inwithclauses. When correspondinguseclauses accompany thewithclauses and the simple names of the library packages are omitted (as is allowed by theuseclause), references to external entities are obscured and identification of external dependencies becomes difficult.In some situations, the benefits of the

useclause are clear. A standard package can be used with the obvious assumption that the reader is very familiar with those packages and that additional overloading will not be introduced.The

use typeclause makes both infix and prefix operators visible without the need forrenamesclauses. You enhance readability with theuse typeclause because you can write statements using the more natural infix notation for operators. See also Guideline 5.7.2.You can minimize the scope of the

useclause by placing it in the body of a package or subprogram or by encapsulating it in a block to restrict visibility.

notesAvoiding the

useclause completely can cause problems with enumeration literals, which must then be fully qualified. This problem can be solved by declaring constants with the enumeration literals as their values, except that such constants cannot be overloaded like enumeration literals.An argument defending the use clause can be found in Rosen (1987).

automation notesThere are tools that can analyze your Ada source code, resolve overloading of names, and automatically convert between the

useclause or fully qualified names.

< Previous Page |
Search | Contents | Index | Next Page > |

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |

TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC | TOC |

Appendix | References | Bibliography |