The following are general guidelines; they do not constitute a complete or precise specification of formatting style. Exercise your judgment in resolving unclear issues.
if statements
try and catch blocks
else should be placed on the
same line as the right brace, if any, that ends the if block.
case arms of a switch statement.
Note that this puts the actual code two levels in from the
opening line.
for loop header, however.
if or
while and its condition, and between a
catch and its parenthesized formal parameter)
These rules are followed, for the most part, by the
Sieve.java and
BounceThread.java
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':
System.out.println("x");
default:
break;
}
while (true) {
boolean done = check();
if (done)
break;
try {
try_again(++i);
} 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;
}