6.2. File Format and Indentation

Template [TEMPLATE]

Use this template for your files. Replace the asterisks by appropriate text. Provide your name, the assignment and version (such as 2A or 3B), the name of the file and the number of columns between tab stops that you have used. (If you do not use tabs, you may write none for tab stops.)

A main function template is provided. Delete it if there is no main function in this file.

Add or remove includes as required.

No long lines [LONG-LINE]

Do not write lines longer than about 80 characters. Break long lines.

Function contracts [CONTRACT-LAYOUT]

Write a contract for each function just before the function definition, without any intervening things unrelated to the function. If a header file contains contracts, write the contract just before the function prototype.

Blank lines [BLANK]

Put a blank line between the end of one function and the contract for the next function. Put a blank line between a function's contract and the function definition. If a contract appears in a header file, put a blank line before and after it and a blank line after the function's prototype. If a function body contains paragraph comments, put a blank line before and after such a comment.

No excessive blank lines [EMPTY-SPACE]

Do not leave excessive blank lines in function bodies or between functions. Do not double-space code. Do put blank lines between major paragraphs of code. Do not double-space header files that do not contain contracts.

Indentation and braces [INDENT]

Every component of a structured statement (if-statemet, while-statement, do-statement, for-statement, switch-statement) must be surrounded by braces and indented either 2 or 3 spaces, as shown in the following example. A right-brace must be directly below the matching left-brace.
  if(condition)
  {
    statement
    ...
    statement
  }
  else
  {
    statement
    ...
    statement
  }

Exception. If the component is and empty set of braces {}, then it is not required to be as shown. For example, you are allowed to write

  for(...) {}
where the empty set of braces is the for-loop body.

Chained ifs [MULTI-IF]

When several if-statements are used to simulate multiple cases, indent all of the cases the same amount, as in the following example.
  if(condition)
  {
    statements
  }
  else if(condition)
  {
    statements
  }
  else if(condition)
  {
    statements
  }
  else
  {
    statements
  }

No incorrect indentation for sequencing [SEQ-INDENT]

Two statements that are performed one after another must be indented the same amount. For example,
  int x;
    int y;
is not correctly indented. Similarly,
  int badindentdemo(int n)
  {
    for(int i = 0; i < n; i++)
    {
      if(test(i))
      {
         return i;
      }
    }
      return -1;
  }
is not correctly indented. Since statement return −1 is performed just after the for-loop (assuming that the return statement inside the for-loop is not done), it should be indented the same amount as the word for.