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. For example, if your tab stops set every 4 spaces, say so. (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. If a header file contains contracts, write the contract just before the function prototype.

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.
  • 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.
  if(condition)
  {
    statement
    ...
    statement
  }
  else
  {
    statement
    ...
    statement
  }

Exception. If the component is and 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 40 points for lack of indentation.


Chained ifs [IF-MULTIPLE-CASES: 1 pt]

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 [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.

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