Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.6 STATEMENTS

5.6.2 Slices

guideline

  • Use slices rather than a loop to copy part of an array.

  • example
    First  : constant Index := Index'First;
    Second : constant Index := Index'Succ(First);
    Third  : constant Index := Index'Succ(Second);
    type Vector is array (Index range <>) of Element;
    subtype Column_Vector is Vector (Index);
    type    Square_Matrix is array  (Index) of Column_Vector;
    subtype Small_Range  is Index range First .. Third;
    subtype Diagonals    is Vector (Small_Range);
    type    Tri_Diagonal is array  (Index) of Diagonals;
    Markov_Probabilities : Square_Matrix;
    Diagonal_Data        : Tri_Diagonal;
    ...
    -- Remove diagonal and off diagonal elements.
    Diagonal_Data(Index'First)(First) := Null_Value;
    Diagonal_Data(Index'First)(Second .. Third) :=
          Markov_Probabilities(Index'First)(First .. Second);
    for I in Second .. Index'Pred(Index'Last) loop
       Diagonal_Data(I) :=
             Markov_Probabilities(I)(Index'Pred(I) .. Index'Succ(I));
    end loop;
    Diagonal_Data(Index'Last)(First .. Second) :=
          Markov_Probabilities(Index'Last)(Index'Pred(Index'Last) .. Index'Last);
    Diagonal_Data(Index'Last)(Third) := Null_Value;
    

    rationale

    An assignment statement with slices is simpler and clearer than a loop and helps the reader see the intended action. See also Guideline 10.5.7 regarding possible performance issues of slice assignments versus loops.


    < 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