Indentation and spacing rules

The following are general guidelines; they do not constitute a complete or precise specification of formatting style. Exercise your judgment in resolving unclear issues.

  1. Use four columns for each successive level of indentation.
  2. Indent the bodies of For methods, place the opening brace on a new line, flush with the method's header. For structured statements, place the opening brace at the end of the opening line. An else should be placed on the same line as the right brace, if any, that ends the if block.
  3. Indent the case arms of a switch statement. Note that this puts the actual code two levels in from the opening line.
  4. Leave one blank line between adjacent class or method declarations.
  5. Start a new line after each statement or declaration, whether terminated by a right brace or a semicolon. Do not start a new line after the internal parts of a for loop header, however.
  6. Leave one blank space between adjacent tokens within the same line, with the following exceptions:

These rules are followed, for the most part, by the and example programs made available during the previous assignment. They are also followed for the most part by the provided C code, to the extent it resembles Java. Note that the rules may not format programs the way you like to write them yourself. Feel free to use your own favorite style in the code you write, but make sure your code produces output that follows the rules described here.

Comments are a little tricky, and not particularly instructional, so I've given you code to handle them. This code enforces the following rules:

Here is some useless code that may give you more sense of the formatting desired (and the already-existing formatting of comments):

  public class foo {    // Note 4 spaces before comment
      // Also note change in indentation, and blank line before method
      public void bar(int arg1, int[] arg2)
      {    // Note brace is on new line
      /* Note blank line after method.  If the next "real" code is another
             method you probably don't want a second line, even if there is
             a comment (like this one) in-between:
       the comment is probably associated with the new method.
         Also, you probably don't want to worry about ugly
   indentation or other aspects of formatting within multi-line
         comments.  Just indent the *first* line of the comment
         appropriately and leave the rest alone. */
      public foo()    // constructor
          for (i = 0; i < limit; i++) {
              if (A[i] > (n = foo(i))) {
                  A[i] = n;
              } else {
                  A[i]--;    // Note no newline before this comment.
              // But there is a newline before this comment, because there
              // was one in the input.  In the previous case there was no
              // newline between the semicolon and the comment in the source.
          switch (c) {
              case 'a':
              case 'b':
                  System.out.println    /* Note newline after this comment */
                  /* (There was one in the input.) */ ("a or b");
              case 'x':
          while (true) {
              boolean done = check();
              if (done)
              try {
              } catch (exc x) {
                  System.out.println("and again");
              } finally {
                  System.out.println("and again!");
      }    // end constructor, followed by blank line
  }    // end foo
  class little_struct {    // Note blank line between classes.
      int a;
      char b;
      float c;

    Back to the parser assignment page

    Back to the grading page

    Back to the course home page

Last Change: 28 October 2004