Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.5 EXPRESSIONS

5.5.2 Array Attributes

guideline

  • Use array attributes 'First , 'Last , or 'Length instead of numeric literals for accessing arrays.
  • Use the 'Range of the array instead of the name of the index subtype to express a range.
  • Use 'Range instead of 'First .. 'Last to express a range.

  • example
    subtype Name_String is String (1 .. Name_Length);
    File_Path : Name_String := (others => ' ');
    ...
    for I in File_Path'Range loop
       ...
    end loop;
    

    rationale

    In the example above, it is better to use Name_String'Range in the for loop than to use Name_String_Size, Name_String'First .. Name_String'Last, or 1 .. 30 because it is clearer, less error-prone, and less dependent on the definitions of Name_String and Name_String_Size. If Name_String is changed to have a different index type or if the bounds of the array are changed, this will still work correctly. This enhances program reliability.


    < 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