Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.6 STATEMENTS

5.6.5 Exit Statements

guideline

  • Use exit statements to enhance the readability of loop termination code (NASA 1987).
  • Use exit when ... rather than if ... then exit whenever possible (NASA 1987).
  • Review exit statement placement.

  • example

    See the examples in Guidelines 5.1.1 and Guidelines 5.6.4.

    rationale

    It is more readable to use exit statements than to try to add Boolean flags to a while loop condition to simulate exits from the middle of a loop. Even if all exit statements would be clustered at the top of the loop body, the separation of a complex condition into multiple exit statements can simplify and make it more readable and clear. The sequential execution of two exit statements is often more clear than the short-circuit control forms.

    The exit when form is preferable to the if ... then exit form because it makes the word exit more visible by not nesting it inside of any control construct. The if ... then exit form is needed only in the case where other statements, in addition to the exit statement, must be executed conditionally. For example:

    Process_Requests:
       loop
          if Status = Done then
    
             Shut_Down;
             exit Process_Requests;
    
          end if;
    
          ...
    
       end loop Process_Requests;
    

    Loops with many scattered exit statements can indicate fuzzy thinking regarding the loop's purpose in the algorithm. Such an algorithm might be coded better some other way, for example, with a series of loops. Some rework can often reduce the number of exit statements and make the code clearer.

    See also Guidelines 5.1.3 and 5.6.4.


    < 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