Ada 95 Quality and Style Guide Chapter 5

Chapter 5: Programming Practices - TOC - 5.6 STATEMENTS

5.6.8 Return Statements

guideline

  • Minimize the number of return statements from a subprogram (NASA 1987).
  • Highlight return statements with comments or white space to keep them from being lost in other code.

  • example

    The following code fragment is longer and more complex than necessary:

    if Pointer /= null then
       if Pointer.Count > 0 then
          return True;
       else  -- Pointer.Count = 0
          return False;
       end if;
    else  -- Pointer = null
       return False;
    end if;
    

    It should be replaced with the shorter, more concise, and clearer equivalent line:

    return Pointer /= null and then Pointer.Count > 0;
    

    rationale

    Excessive use of returns can make code confusing and unreadable. Only use return statements where warranted. Too many returns from a subprogram may be an indicator of cluttered logic. If the application requires multiple returns, use them at the same level (i.e., as in different branches of a case statement), rather than scattered throughout the subprogram code. Some rework can often reduce the number of return statements to one and make the code more clear.

    exceptions

    Do not avoid return statements if it detracts from natural structure and code readability.


    < 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