CSc 210 Basic Style and Documentation Guidelines

Style

    1.   All variables should begin with lower case characters.
    2.   All constants and enumerations should be in all upper case.
    3.   All types should be begin in lower case and then move to mixed case.
    4.   All function/method identifiers should be in full mixed case (i.e., each word will begin with a capital and the rest of the word will be lower case).
    5.   Functional indentation should be used in all control structures. Indentation stops should be consistent and should be 3 or 4 horizontal spaces.
    6.   Horizontal spacing should be used for clarity of readability:
            1.between operators and operands (e.g., 4 + 6 / 2 )
            2.between control structures and their conditionals (e.g., if ( a ) { )
            3.between arguments (i.e., after commas)
            4.to line up arguments, as in declarations, assignments, multi-line logical expressions
            5.between control structure headers and their expressions (this is indentation, use the same indentation
              amount consistently)
    7.   Vertical spacing should be used for readability and clarity:
            1.between documentation banners and function headers (1 line).
            2.between statement blocks of different types (e.g., declarations and assignments, includes and defines, I/O
              statements and other statements, control structures and other statements)
            3.between compound statement curly braces and other statements (line feed only)
            4.between executable code and function/method return statements, for clarity
    8.   All functions/methods (except constructors/destructors) should have type declarations and return types (even if
         void). This is for consistency.
    9.   All functions/methods should have parameter lists (even if void). This is for consistency.
    10.   There should only be a single executable expression per line. No executable expressions should be on the same line as a control structure header (e.g., while ( foo ) { a = b; } is not good style).
Documentation

       1.   All documentation should be placed in banners ABOVE the related code fragment. No comments should be in
     in-line in submissions. In-line comments are for debugging. Code that is so long that comments are needed to
     break it up has not used abstraction e ffectively and should be redesigned.

     'In-line' comments means comments that share a line with executable code. In general, the use of comments
     within an executable block of code is discouraged because of the above reason. In general, if in-block comments
     are used, they should be blocked off the same way as banner comments, to maintain continuity.

      2.   Contents of banners:

          //********************************************************
          // FILE:             As it is in the current directory
          //
          // AUTHOR:           Whomever + citations
          //
          // USAGE COMMENTS:   On compilation, usage, etc.
          //
          // LIBRARIES:        Included libraries and why
          //
          // TYPES DEFINED:    All typedefs, enumerations, classes
          //
          // CONSTANTS DEFINED:
          //
          // FUNCTIONS/METHODS DECLARED:
          //
          //   void    Myfun1 --  Description of MyFun1
          //   int       Myfun2 --  Description of MyFun2
          //   Boolean Myfun3 --  Description of MyFun3, this one
          //                      carries over to another line
          //   void     Myfun4 --  Description of MyFun4
          //   myClass Myfun5 --  Description of MyFun5
          //
          // GLOBAL VARIABLES USED:
          //
          // GENERAL COMMENTS:
          //
          //********************************************************
 

       3.   Function/Method Prototypes (example template):

          Documentation for function prototypes, being an interface only need to tell the reader what the function
          expects and what they can expect from the function. The header will tell them the parameter types and
          their order, as well as the function return type , but these can also be included in the documentation for
          clarity.
 

          //********************************************************
          // IDENTIFER: FUNCTION Of Function/Method
          //
          // PRE:       Precondition of the function/method
          //                (What conditions are assummed to true prior to calling.)
          //
          // POST:      Postcondition of the method
          //                (What conditions are now true when returning,
          //                  i.e. state any side effects of the function)
          //
          // INPUT:     Description of input parameters
          //
          // OUTPUT:    Descriptions of output parameters
          //
          // RETURN:    Value returned by the function/method
          //
          //********************************************************
 
        EXAMPLE: BUBBLE SORT
 

          //*****************************************************
          // FILE:   BubbleSort.C: Driver that sorts the items
          //                       in an array into ascending
          //                       order.
          //
          // AUTHOR: Frank Carrano, Chapter 9, pp 410-411
          //
          // LIBRARIES:   stdlib.h - for rand
          //              iostream.h - standard I/O
          //
          // FUNCTIONS DEFINED:
          //   void    BubbleSort --  Sorts an array
          //   void     Swap --  swaps two array elements
          //
          // VARIABLES: int       n  - loop counter
          //            arrayType a  - the array to sort
          //
          // General Comment:
          //    This program randomly generates an array of
          // integer elements and calls the bubble sort routine
          // to sort the array.
          //
          //*****************************************************

          #include <iostream.h>
          #include <stdlib.h>

          #define MAXSZ 12

          typedef int dataType;
          typedef dataType arrayType[MAXSZ];

          void BubbleSort(dataType a[MAXSZ]);
          void Swap(dataType& x, dataType& y);

          int
          main(void)
          {
             arrayType a;
             int       n;

             for ( n = 0; n < MAXSZ; n++ )
                a[n] = rand();

             cout << "The array before sorting is:\n\n";

             for ( n = 0; n < MAXSZ; n++ )
                cout << a[n] << endl;

             cout << endl;

             BubbleSort(a);

             cout << "The array after sorting is:\n\n";

             for ( n = 0; n < MAXSZ; n++ )
                cout << a[n] << endl;

             cout << endl;

             return(0);
          }

          //*****************************************************
          // BubbleSort.C: Sorts the items in an array into
          //               ascending order.
          //
          // PRE:   a is an array of MAXSZ items
          // POST:  a is sorted into ascending order
          //
          // TYPES:     enum boolean {FALSE, TRUE}
          //
          // VARIABLES: boolean sorted    - logical result
          //            int     pass      - current outer loop
          //            int     index     - lower comparison
          //                                index
          //            int     nextIndex - upper comparison
          //                                index
          //
          // COMMENTS:
          //   Note that initially, the sorted regions is a[0] and the
          //   unsorted region is a[sorted..n - 1]. In
          //   general, the sorted region is a[0..unsorted - 1]
          //   and the unsorted region is a[unsorted..n - 1]
          //
          //*****************************************************

          void
          BubbleSort(dataType a[MAXSZ])
          {
             enum    boolean {FALSE, TRUE};
             boolean sorted = FALSE;
             int     pass, index, nextIndex;

             for ( pass = 1; (pass < MAXSZ) && !sorted; pass++ ) {
                sorted = TRUE;

                for ( index = 0; index < MAXSZ - pass; index++ ) {
                   nextIndex = index + 1;

                   if ( a[index] > a[nextIndex] ) {
                      Swap(a[index], a[nextIndex]);
                      sorted = FALSE;
                   }
                }
             }

             return;
          }

          //***********************************************************
          // Swap: Swaps x and y
          //
          // PRE:    None.
          // INPUT:  dataType& x, y -- data to exchange
          // LOCAL:  dataType  temp -- temporary data
          // OUTPUT: dataType& x, y -- swapped data
          // POST:   Contents of actual locations that x and y
          //         represent are swapped.
          //
          //***********************************************************

          void
          Swap(dataType& x, dataType& y)
          {
             dataType temp;

             temp = x;
             x    = y;
             y    = temp;

             return;
          }