Ada 95 QUALITY AND STYLE SOURCE CODE PRESENTATION CHAPTER 2 Source Code Presentation 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. 2.1 CODE FORMATTING 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 • Use consistent spacing around delimiters. • Use the same spacing as you would in regular prose. instantiation Specifically, leave at least one blank space in the following places, as shown in the examples throughout this book. More spaces may be required for the vertical alignment recommended in subsequent guidelines. - Before and after the following delimiters and binary operators: + - * / & < = > /= <= >= := => | .. : <> - Outside of the quotes for string (“) and character (‘) literals, except where prohibited. - Outside, but not inside, parentheses. - After commas (,) and semicolons (;). Do not leave any blank spaces in the following places, even if this conflicts with the above recommendations. - After the plus (+) and minus (-) signs when used as unary operators. - After a function call. - Inside of label delimiters (<< >>). - Before and after the exponentiation operator (**), apostrophe (‘), and period (.) - Between multiple consecutive opening or closing parentheses. - Before commas (,) and semicolons (;). When superfluous parentheses are omitted because of operator precedence rules, spaces may optionally be removed around the highest precedence operators in that expression. example Default_String : constant String := “This is the long string returned by” & “ default. It is broken into multiple” & “ Ada source lines for convenience.”; type Signed_Whole_16 is range -2**15 .. 2**15 - 1; type Address_Area is array (Natural range <>) of Signed_Whole_16; Register : Address_Area (16#7FF0# .. 16#7FFF#); Memory : Address_Area ( 0 .. 16#7FEC#); Register(Pc) := Register(A); X := Signed_Whole_16(Radius * Sin(Angle)); Register(Index) := Memory(Base_Address + Index * Element_Length); Get(Value => Sensor); Error_Term := 1.0 - (Cos(Theta)**2 + Sin(Theta)**2); Z := X**3; Y := C * X + B; Volume := Length * Width * Height; rationale It is a good idea to use white space around delimiters and operators because they are typically short sequences (one or two characters) that can easily get lost among the longer keywords and identifiers. Putting white space around them makes them stand out. Consistency in spacing also helps make the source code easier to scan visually. However, many of the delimiters (commas, semicolons, parentheses, etc.) are familiar as normal punctuation marks. It is distracting to see them spaced differently in a computer program than in normal text. Therefore, use the same spacing as in text (no spaces before commas and semicolons, no spaces inside parentheses, etc.). exceptions The one notable exception is the colon (:). In Ada, it is useful to use the colon as a tabulator or a column separator (see Guideline 2.1.4). In this context, it makes sense to put spaces before and after the colon rather than only after it as in normal text. automation notes The guidelines in this section are easily enforced with an automatic code formatter. 2.1.2 Indentation guideline • Indent and align nested control structures, continuation lines, and embedded units consistently. • Distinguish between indentation for nested control structures and for continuation lines. • Use spaces for indentation, not the tab character (Nissen and Wallis 1984, §2.2). instantiation Specifically, the following indentation conventions are recommended, as shown in the examples throughout this book. Note that the minimum indentation is described. More spaces may be required for the vertical alignment recommended in subsequent guidelines. - Use the recommended paragraphing shown in the Ada Reference Manual (1995). - Use three spaces as the basic unit of indentation for nesting. - Use two spaces as the basic unit of indentation for continuation lines. A label is outdented three spaces: begin <