Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.6 STATEMENTS

5.6.9 Blocks

guideline

  • Use blocks to localize the scope of declarations.
  • Use blocks to perform local renaming.
  • Use blocks to define local exception handlers.

  • example
    with Motion;
    with Accelerometer_Device;
    ...
    
       ---------------------------------------------------------------------
       function Maximum_Velocity return Motion.Velocity is
    
          Cumulative : Motion.Velocity := 0.0;
    
       begin  -- Maximum_Velocity
    
          -- Initialize the needed devices
          ...
    
          Calculate_Velocity_From_Sample_Data:
             declare
                use type Motion.Acceleration;
    
                Current       : Motion.Acceleration := 0.0;
                Time_Delta    : Duration;
    
             begin  -- Calculate_Velocity_From_Sample_Data
                for I in 1 .. Accelerometer_Device.Sample_Limit loop
    
                   Get_Samples_And_Ignore_Invalid_Data:
                      begin
                         Accelerometer_Device.Get_Value(Current, Time_Delta);
                      exception
                         when Constraint_Error =>
                            null; -- Continue trying
    
                         when Accelerometer_Device.Failure =>
                            raise Accelerometer_Device_Failed;
                      end Get_Samples_And_Ignore_Invalid_Data;
    
                   exit when Current <= 0.0; -- Slowing down
    
                   Update_Velocity:
                      declare
                         use type Motion.Velocity;
                         use type Motion.Acceleration;
    
                      begin
                         Cumulative := Cumulative + Current * Time_Delta;
    
                      exception
                         when Constraint_Error =>
                            raise Maximum_Velocity_Exceeded;
                      end Update_Velocity;
    
                end loop;
             end Calculate_Velocity_From_Sample_Data;
    
          return Cumulative;
    
       end Maximum_Velocity;
       ---------------------------------------------------------------------
    ...
    

    rationale

    Blocks break up large segments of code and isolate details relevant to each subsection of code. Variables that are only used in a particular section of code are clearly visible when a declarative block delineates that code.

    Renaming may simplify the expression of algorithms and enhance readability for a given section of code. But it is confusing when a renames clause is visually separated from the code to which it applies. The declarative region allows the renames to be immediately visible when the reader is examining code that uses that abbreviation. Guideline 5.7.1 discusses a similar guideline concerning the use clause.

    Local exception handlers can catch exceptions close to the point of origin and allow them to be either handled, propagated, or converted.


    < 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