6.3. File Format and Indentation

Template [Template: 1 point]

Use the template(s) given with each assignment for source files. Replace the asterisks by appropriate text. Provide your name, the assignment number, the name of the file and the number of columns between tab stops that you have used.

Show tab stops, if used [Tabs: 1 points]

If you use tabs, you must say, in the heading at the top of the file, where the tab stops are. Just give one integer, the spacing between tab stops. For example,
  // tabs: 4
indicates that tab stops are 4 characters apart. (If you do not use tabs, you may write none for tab stops.)

Pay attention to this! If you specify the wrong spacing of tab stops, your program might be very poorly indented, and you will lose points for indentation.


No long lines [Long line: 1-2 points]

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

Function contracts [Contract layout: 1 point]

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

Blank lines [Use blank lines: 1-2 points]

  • 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.
  • There should be a blank line before and after each type 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 [Extra empty space: 1 point]

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

Indentation and braces [Indent: 1-10 points*]

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, with nothing on or between the line segment from the left brace to the right brace.

  if(condition)
  {
    statement
    ...
    statement
  }
  else
  {
    statement
    ...
    statement
  }

Exception. If the component is an empty set of braces {}, then it is not required to be on separate lines. For example, you are allowed to write

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

*A program that has exceptionally poor indentation throughout can lose up to 50 points for lack of indentation.


No incorrect indentation for sequencing [Indent sequence: 1-10 points*]

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.

This counts as an Indent error.

*A program that has exceptionally poor indentation throughout can lose up to 50 points for lack of indentation.


Chained ifs [If layout: 1-2 pts]

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
  }