File: style-guide.texi

package info (click to toggle)
libaws 2.2dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,624 kB
  • ctags: 1,173
  • sloc: ada: 61,829; ansic: 6,483; makefile: 1,282; xml: 196; sh: 119; java: 112; python: 66; sed: 40
file content (669 lines) | stat: -rw-r--r-- 18,005 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
\input texinfo   @c -*-texinfo-*-

@c %**start of header

@c
@c AWS Coding Style Guide
@c
@c Note that this document is derived from the GNAT Style Guide from
@c Ada Core Technologies, Inc. Some modifications have been made to
@c meet AWS developement requirement.
@c
@c The "cut&past" from the GNAT Coding Style has been made with ACT
@c permission.
@c

@setfilename style-guide.info
@settitle AWS Coding Style
@c %**end of header

@ifinfo
     @center AWS Coding Style

     @center A guide for AWS developers
@end ifinfo

@titlepage
@sp 10
@title AWS Coding Style
@subtitle A guide for AWS developers
@subtitle Document revision level $Revision: 1.10 $
@subtitle Date: @today{}
@author Pascal Obry.
@end titlepage
@raisesections

@node    Top,       General,         , (dir)
@comment node-name, next,    previous, up

@menu
* General::
* Lexical Elements::
* Declarations and Types::
* Expressions and Names::
* Statements::
* Subprograms::
* Packages::
* Program Structure::
@end menu

@c  -------------------------------------------------------------------------
@node    General, Lexical Elements, Top, Top
@section General
@c  -------------------------------------------------------------------------

@noindent
This document described the style rules for the development of the AWS
project. The goal is to have a consistent style used for all AWS
codes.

@c  -------------------------------------------------------------------------
@node    Lexical Elements, Declarations and Types, General, Top
@section Lexical Elements
@c  -------------------------------------------------------------------------

@subsection Character Set and Separators
@c  -------------------------------------------------------------------------

@itemize @bullet
@item
The character set used should be plain 7-bit ASCII.
The only separators allowed are space and the end-of-line sequence. 
No other control character or format effector (such as HT, VT, FF) 
should be used.

The end-of-line sequence used must be the standard UNIX end-of-line
character, a single LF (16#0A#).

@item
A line should never be longer than 79 characters, not counting the line
separator.

@item
Lines must not have trailing blanks.

@item
Indentation is 3 characters per level for if-statements, loops, case
statements.

@end itemize

@subsection Identifiers
@c  -------------------------------------------------------------------------
@itemize @bullet
@item
Identifiers will start with an upper case letter, and each letter following
an underscore will be upper case.  Short acronyms may be all upper case. 
All other letters are lower case.  
An exception is for identifiers matching a foreign language. In particular,
we use all lower case where appropriate for C.

@item
Use underscores to separate words in an identifier.  

@item Try to limit your use of abbreviations in identifiers. 
It is ok to make a few abbreviations, explain what they mean, and then
use them frequently, but don't use lots of obscure abbreviations.

@item
Don't use the variable I, use J instead, I is too easily mixed up with
1 in some fonts. Similarly don't use the variable O, which is too easily
mixed up with zero.
@end itemize

@subsection Numeric Literals
@c  -------------------------------------------------------------------------
@itemize @bullet
@item
Numeric literals should include underscores where helpful for
readability.

@smallexample
      1_000_000
      16#8000_000#
      3.14159_26535_89793_23846
@end smallexample
@end itemize

@subsection Reserved Words
@c  -------------------------------------------------------------------------
@itemize @bullet
@item
Reserved words use all lower case.

@smallexample
       return else procedure
@end smallexample

@item
The words "@b{Access}", "@b{Delta}" and "@b{Digits}" are capitalized
when used as attribute_designator.
@end itemize

@subsection Comments
@c  -------------------------------------------------------------------------

@itemize @bullet
@item
Comment start with @code{--  } (ie @code{--} followed by two spaces).
The only exception to this rule (i.e. one space is tolerated) is when the
comment ends with @code{--}.
It also accepted to have only one space between @code{--} and the start 
of the comment when the comment is at the end of a line, 
after an Ada statement.

@item
Every sentence in a comment should start with an upper-case letter (including
the first letter of the comment).

@item
When declarations are commented with "hanging" comments, i.e. comments
after the declaration, there is no blank line before the comment, and
if it is absolutely necessary to have blank lines within the comments
these blank lines *do* have a -- (unlike the normal rule, which is to
use entirely blank lines for separating comment paragraphs).
The comment start at same level of indentation as code they are commenting.

@smallexample
       Z : Integer;
       --  @i{Integer value for storing value of Z}
       --
       --  @i{The previous line was a blank line}
@end smallexample

@item
Comments that are dubious or incomplete or comment on possibly
wrong or incomplete code should be preceded or followed by ???

@item
Comments in a subprogram body must generally be surrounded by blank lines,
except after a "@b{begin}":

@smallexample
       @b{begin}
          --  @i{Comment for the next statement}

          A := 5;

          --  @i{Comment for the B statement}

          B := 6;
@end smallexample

@item
In sequences of statements, comments at the end of the lines should be
aligned.

@smallexample
        My_Identifier := 5;      --  @i{First comment}
        Other_Id := 6;           --  @i{Second comment}
@end smallexample

@item
Short comments that fit on a single line are NOT ended with a period.
Comments taking more than a line are punctuated in the normal manner.

@item
Comments should focus on why instead of what.
Descriptions of what subprograms do go with the specification.

@item
Comments describing a subprogram spec should specifically mention the
formal argument names. General rule: write a comment that does not
depend on the names of things. The names are supplementary, not
sufficient, as comments.

@item
Do NOT put two spaces after periods in comments.
@end itemize

@c  -------------------------------------------------------------------------
@node    Declarations and Types, Expressions and Names, Lexical Elements,Top
@section Declarations and Types
@c  -------------------------------------------------------------------------

@itemize @bullet
@item
In entity declarations, colons must be surrounded by spaces. Colons
should be aligned.

@smallexample
        Entity1   : Integer;
        My_Entity : Integer;
@end smallexample

@item
Declarations should be grouped in a logical order. 
Related groups of declarations may be preceded by a header comment.

@item
All local subprograms in a subprogram or package body should be declared
before the first local subprogram body. 

@item
Avoid declaring discriminated record types where the discriminant is used
for constraining an unconstrained array type. (Discriminated
records for a variant part are allowed.)

@item
Avoid declaring local entities that hide global entities.

@item
Don't declare multiple variables in one declaration that spans lines. 
Start a new declaration on each line, instead

@item
The defining_identifiers of global declarations serve as comments of a sort. 
So don't choose terse names, but look for names that give useful information 
instead.

@item
Local names can be shorter, because they are used only within 
one context, where comments explain their purpose.

@end itemize


@c  -------------------------------------------------------------------------
@node    Expressions and Names, Statements, Declarations and Types, Top
@section Expressions and Names
@c  -------------------------------------------------------------------------

@itemize @bullet

@item
Every operator must be surrounded by spaces, except for the 
exponentiation operator.

@smallexample
       E := A * B**2 + 3 * (C - D);
@end smallexample

@item
When folding a long line, fold before an operator, not after.

@item
Use parentheses where they clarify the intended association of
operands with operators:
@smallexample
       (A / B) * C
@end smallexample
@end itemize

@c  -------------------------------------------------------------------------
@node    Statements, Subprograms, Expressions and Names, Top
@section Statements
@c  -------------------------------------------------------------------------

@subsection Simple and Compound Statements
@c  -------------------------------------------------------------------------
@itemize @bullet
@item
Use only one statement or label per line.
@item
A longer sequence_of_statements may be divided in logical groups
or separated from surrounding code using a blank line.

@item Prefer using "/=" to "not =" except in complex expression if it
makes the expression easier to read or in "well-known" expressions for
whose the reverse must be checked.
@end itemize

@subsection If Statements
@c  -------------------------------------------------------------------------
@itemize @bullet
@item
When the "@b{if}", "@b{elsif}" or "@b{else}" keywords fit on the same
line with the condition and the "@b{then}" keyword, then the statement is
formatted as follows:

@smallexample
        @b{if} <condition> @b{then}
           ...
        @b{elsif} <condition> @b{then}
           ...
        @b{else}
           ...
        @b{end if};
@end smallexample

@noindent
When the above layout is not possible, "@b{then}" should be aligned
with "@b{if}", and conditions should preferably be split before an
"@b{and}" or "@b{or}" keyword a follows:

@smallexample
        @b{if} <long_condition_that_has_to_be_split>
          @b{and then} <continued_on_the_next_line>
        @b{then}
           ...
        @b{end if};
@end smallexample

@noindent
The "@b{elsif}", "@b{else}" and "@b{end if}" always line up with the
"@b{if}" keyword. The preferred location for splitting the line is
before "@b{and}" or "@b{or}". The continuation of a condition is
indented with two spaces or as many as needed to make nesting clear.

@smallexample
     @b{if} x = lakdsjfhlkashfdlkflkdsalkhfsalkdhflkjdsahf
          @b{or else}
        x = asldkjhalkdsjfhhfd
          @b{or else}
        x = asdfadsfadsf
     @b{then}
@end smallexample

@item
Conditions should use short-circuit forms ("@b{and then}", "@b{or else}").

@item
Complex conditions in if-statements are indented two characters:

@smallexample
      @b{if} this_complex_condition
        @b{and then} that_other_one
        @b{and then} one_last_one
      @b{then}
         ...
@end smallexample

@item
Every "@b{if}" block is preceded and followed by a blank line, except
where it begins or ends a sequence_of_statements.

@smallexample
        A := 5;

        @b{if} A = 5 @b{then}
           null;
        @b{end if};

        A := 6;
@end smallexample
@end itemize

@subsection Case statements
@itemize @bullet

@item
Layout is as below.

@smallexample
       @b{case} <expression> @b{is}
          @b{when} <condition> @b{=>}
             ...
          @b{when} <condition> @b{=>}
             ...
       @b{end case};
@end smallexample

@noindent
If the condition and the code for the case section is small, it is
possible to put the code for each when section right after the
condition without a new-line.

@smallexample
       @b{case} <expression> @b{is}
          @b{when} <condition> @b{=>} ...
          @b{when} <condition> @b{=>} ...
       @b{end case};
@end smallexample

@end itemize

@subsection Loop statements
@itemize @bullet

@noindent
When possible, have "@b{for}" or "@b{while}" on one line with the condition
and the "@b{loop}" keyword.

@smallexample
       @b{for} J @b{in} S'@b{Range} @b{loop}
          ...
       @b{end loop};
@end smallexample

@noindent
If the condition is too long, split the condition (see if_statement)
and align "@b{loop}" with the "@b{for}" or "@b{while}" keyword.

@smallexample
      @b{while} <long_condition_that_has_to_be_split>
        @b{and then} <continued_on_the_next_line>
      @b{loop}
         ...
      @b{end loop};
@end smallexample

@noindent
If the loop_statement has an identifier, it is layout as follows:

@smallexample
      Outer : @b{while not} <condition> @b{loop}
         ...
      @b{end} Outer;
@end smallexample
@end itemize

@subsection Block Statements
@itemize @bullet

@item
The (optional) "@b{declare}", "@b{begin}" and "@b{end}" statements are aligned,
except when the block_statement is named:

@smallexample
      Some_Block : @b{declare}
         ...
      @b{begin}
         ...
      @b{end} Some_Block;
@end smallexample

@end itemize

@c  -------------------------------------------------------------------------
@node    Subprograms, Packages, Statements, Top
@section Subprograms
@c  -------------------------------------------------------------------------


@subsection Subprogram Declarations
@c  -------------------------------------------------------------------------
@itemize @bullet

@item
Always write the "@b{in}" for parameters, even in functions:

@smallexample
      @b{function} Length (S : @b{in} String) @b{return} Integer;
@end smallexample

@item The mode should be indented as follow

@smallexample
      @b{procedure} My_Proc
        (First  : @b{in}     Integer;
         Second :    @b{out} Character;
         Third  : @b{access} String;
         Fourth : @b{in out} Float);
@end smallexample

@item
When the declaration line for a procedure or a function is too long, fold it

@smallexample
      @b{function} Head
        (Source : @b{in} String;
         Count  : @b{in} Natural;
         Pad    : @b{in} Character := Space)
         @b{return} String;
@end smallexample

@item For function an alternate style is to put the @b{return} at the end of
the last declaration line

@smallexample
      @b{function} Head
        (Source : @b{in} String;
         Count  : @b{in} Natural;
         Pad    : @b{in} Character := Space) @b{return} String;
@end smallexample

@item
The parameter list for a subprogram is preceded by a space

@smallexample
        @b{procedure} Func (A : @b{in out} Integer);
@end smallexample

@end itemize

@subsection Subprogram Bodies
@c  -------------------------------------------------------------------------
@itemize @bullet

@item
The functions and procedures should always be sorted alphabetically in
a compilation unit.

@item
All subprograms have a header giving the function name, with the following
format:

@smallexample
      -----------------
      -- My_Function --
      -----------------

      @b{procedure} My_Function @b{is}
      @b{begin}
@end smallexample

Note that the name in the header is preceded by a single space, 
not two spaces as for other comments.

@item If the subprogram parameters are on multiple lines and there is
some declaration the "@b{is}" must be on a separate line.

@smallexample
      @b{procedure} My_Function (X : @b{in} Integer) @b{is}
         X : Float;
      @b{begin}

      @b{procedure} My_Function 
        (X : @b{in} Integer;
         Y : @b{in} Float)
      @b{is}
         A : Character;
      @b{begin}
@end smallexample

@item
Every subprogram body must have a preceding subprogram_declaration.

@item
If declarations of a subprogram contain at least one nested subprogram
body, then just before the begin is a line:

@smallexample
        --  @i{Start of processing for bla bla}

	@b{begin}
@end smallexample

@end itemize

@c  -------------------------------------------------------------------------
@node    Packages, Program Structure, Subprograms, Top
@section Packages and Visibility Rules
@c  -------------------------------------------------------------------------

@itemize @bullet

@item
All program units and subprograms have their name at the end:

@smallexample
      @b{package} P @b{is}
         ...
      @b{end} P;
@end smallexample

@item
Avoid "use-ing" the with-ed packages except when it has been designed
for. A common example is Ada.Strings.Unbounded where the type is
named Unbounded_String. This unit is clearly designed to be use-ed. To
ease readability a use clause may be used in a small scope. Another
solution is to use renaming. Do not with two times the same unit,
always use the deepest child unit to with. For example do not write:

@smallexample
      @b{with} Ada.Strings;
      @b{with} Ada.Strings.Unbounded;
@end smallexample

but the equivalent form:

@smallexample
      @b{with} Ada.Strings.Unbounded;
@end smallexample

@item
Names declared in the visible part of packages should be
unique, to prevent name clashes when the packages are "use"d.

@smallexample
      @b{package} Entity @b{is}
         @b{type} Entity_Kind @b{is} ...;
	 ...
      @b{end} Entity;
@end smallexample

@item
After the file header comment, the context clause and unit specification
should be the first thing in a program_unit.

@item try grouping the context clauses

It is good to group the context clauses in 3 parts. The Ada standard
clauses, the components from other projects and then the project's
clauses. In each group it is required to sort the clauses by
alphabetical order.

@smallexample
      @b{with} Ada.Exceptions;
      @b{with} Ada.Strings;

      @b{with} Lists;
      @b{with} Ordered_Set;

      @b{with} AWS.Server;
      @b{with} AWS.URL;
@end smallexample

@end itemize

@c  -------------------------------------------------------------------------
@node    Program Structure,, Packages, Top
@section Program Structure and Compilation Issues
@c  -------------------------------------------------------------------------

@itemize @bullet
@item
Every AWS source file must be compiled with the 
"@b{-gnatwcfijmpruv -gnatwe -gnaty3abcefhiklmnoprst}" switches to
check the coding style.

@item
Each source file should contain only one compilation unit.

@item
Body filename should end with ".adb" and spec with ".ads".

@end itemize

@bye