Layout and Style
The prime purpose of a good layout is to make the logical structure of the code more obvious to people. 

Good layout must be:
          - accurately represent the logical structure of the code 
          - consistent represent the logical structure of the code 
          - improved readability 
          - improved maintainability 
          - "looking good " is not important
 

Fundamental Theorem of Formating
          A visual layout is considered good if and only if it shows the logical structure of the program.
Layout Techniques

-White Space
          - it groups related items together
          - it separates unrelated items
          - it aligns elements that belong together
          - it indent statements that are logically related

- Parentheses
          - it adds clarity to the code which can improve readability of the code
          - use more Parentheses
 

Layout Styles

- Pure Blocks
          - A well designed language has clear block structures that lend themselves to a natural indent style.
          - This layout styles is only for the Ada.

            Exmaple:
            while PixelColor = RedColor loop
                statement1;
                statement2;
                ...
            end loop;
            //in this example, the beginning of loop(while) should be aligned with its end(end).

- Endline Layout
          - Refers a large group of layout strategies in which the code is intented almost to the end of the line.
          - The endline indentation is used to align a block with the keyword that began it, to make a routine's 
            subsequent parameters line up under its first parameter, and to line up cases in a case statement.

            Example:
            if (SoldCount > 100) then begin
                   Markdown := 0.10;
                   Profit          := 0.05;
                   end
            else Markdown := 0.05; 

- Emulating Pure Blocks
          - A better alternative to using endline layout in languages that don'y have pure blocks is to view the begin 
            and end keywords as extensions of the control construct they are used with.

            Example:
            case PixelColor of
                RedColor: begin
                    statement1;
                    statement2;
                    ...
                end;
                GreenColor: begin
                    statement1;
                    statement2;
                    ...
                end;
            end
 

- begin and end as Block Boundaries
          - A substitute for a pure block structure is to view begin-end pairsas block boundies. If you take that 
            approach, you view the begin and the end as statements that follow the control construct rather then 
            as fragment that are part of it.

            Example:
            if PixelColor = RedColor do
                begin
                statement1;
                statement2;
                ....
                end;
 

Layout of Individual Statements

- Limits the length of the statement line length to 80 characters and use only one statement per line
- Formating continuation lines, end each line at where a normal statement would not end.

   Example:
                                  TotalAmount = TotalAmount + CustomerPurchase - 
                                                            CustomerDiscount + Taxes;

- Indent assignment to a point after the "="

   Example:
              MyClass.Total    =    Inventory + Purchses
              MyClass.Sale     =     Sales
              MyClass.Salary  =     Hours * HourlyRate - EI
 

- Align Data Declarations, and use one data declaration per line. 
  Always declare data at the start of the code block.

   Example:
             int                 Inventory;
             int                 Purchases;
             double          Taxes;
             PartTime       HourlyRate;

- Align Pointer and reference declarations

   Example:
             int         *      pInventory;
             float      *      pPurchases;
             int        &      rTaxes;
             int                 HourlyRate;

- Indent a comment with its corresponding code

   Example:
           // If the user enter 999, then exit the program
          if (num = 999)
          {
                 // Display end of program message.
                cout  << "Bye-Bye";
                exit();
          }
 

Example:

JAVA           good  bad
C                   good  bad
C++              good  bad



Authors: Alice Mok, Rena Xu, Mandy Wong, Anthony Leung,  Andy Ho, Gary Lo
Reference: Code Complete, A Practical Handbook of Software Construction. Steve McConnell. Microsoft Press (1993)
Last Modified: June 15, 2000
Copyright 2000 Department of Computer Science, University of Regina.


[CS Dept Home Page]