Guidelines for Style on Projects
1. Use Proper Identifiers
Camel-Case
Identifiers for classes, interfaces, and enumerations should use "upper camel-case" style. These
identifiers consist of words that are jammed together, each word beginning with an upper-case letter. Examples
of correct upper camel-case are:
AntEater NumberFormatException String DogCatcher
Identifiers for packages and most variables should use "lower camel-case" style. This style is the same as "upper camel-case"
except that the identifier begins with a lower-case letter. Examples of correct lower camel-case are:
numberOfCats animals currentItem radiationLevel
Named Constants
For most literal values appearing in your code, it is best to used "named constants". For example, rather than this:
double circumference = 2 * 3.1415927 * radius;
we prefer to declare a variable like this:
final double PI = 3.1415927;
and then write the statement as follows:
double circumference = 2 * PI * radius;
Note that the style for variables serving as "named constants" utilizes all capital letters. Words should be separated by
the underscore character. Examples of the correct style for named constants are:
WHEELS_ON_BICYCLE BACKGROUND_COLOR PI MAXIMUM_RADIUS
Meaningful English Words
Identifiers should almost always consist of English words that describe the role of the entity in the program. You should avoid
identifiers like the following bad examples:
x1 t bgSp temp7 smNu
What in the world do those identifiers represent? Who would know?! Identifiers should primarily consist of English words jammed
together. That doesn't mean you can never use abbreviations. For example, any of the following identifers seem OK to me:
currentRectangle currRectangle currRect
That last one might be a stretch, but I think it's clear enough.
Can I Ever Use a Single-Letter Variable?
There are cases where one might correctly use a variable consisting of just one letter, but they are rare. When using a conventional for-loop
to iterate over a collection, it is common to see the variable i as an abbreviation for
index:
for (int i = 0; i < a.length; i++) {...}
However, in most cases this is not the best style, since a for-each loop is more readable and supresses the need for an index variable. In cases
where an index variable is necessary, there is often a better choice for the identifier (e.g. "row", or "col"). We will accept the variable "i" as
an identifier only in cases where a for-each loop is not sufficient, and there really is no better choice from context.
There are cases (beyond looping) where a single-letter variable's role is clear from context.
For example, when implementing graphical applications, it is common to use the variables
x and y to specify coordinates.
2. Proper Braces and Indentation
We are very fussy about this! Below are examples of correctly placed braces and indentation for
many different types of Java statements. You should not be using any other style.
Correct Style Examples for if and if-else
if (.....) {
....
....
}
if (.....) {
....
....
} else {
....
....
....
}
if (.....) {
....
....
} else if (.....) {
....
....
} else if (.....) {
....
....
}
if (.....) {
....
....
} else if (.....) {
....
....
} else {
....
....
}
Correct Style for loops
for(int i = 0; i < a.length; i++) {
....
....
}
while(.....) {
....
....
}
do {
....
.....
} while(.....);
Correct style for switch
Note that you do not indent the cases. This always looks weird to me, but it is considered the correct style.
switch(x) {
case 7:
....
....
case 28:
....
....
default:
....
....
}
Incorrect Style Example
The opening brace for the body of a clause must not appear on the line below! Although this
style is commonly used by C and C++ programmers, it is frowned upon by Java programmers (and you'll lose points
in this course if you use it!)
Always use Braces!
The following code fragment looks great:
if (x > 10) {
System.out.println("x is large");
}
Don't be lazy and leave off the braces. Even though the following code fragment is functionally equivalent to the above,
it is error-prone and considered bad style:
if (x > 10)
System.out.println("x is large");
3. Reasonable Line Lengths
The policy this semester will be that no line of code in your project submissions should exceed the 120th column. This rule
applies to everything, including import statements and comments.
4. Eliminating Redundancy
You should always strive to eliminate redundancy in your code. We should not see separate chunks of code that perform
essentially the same task. The reason we care about this is not because we want our code to be concise; it is because redundancy
makes it very hard to maintain code. You will lose points in this course if your code is excessively redundant.
5. Proper Commenting
We will be grading the quality of your commenting. We expect to see comments in all of the following places:
At the top of every class, interface, or enum.
There should be a brief comment at the top of every module that describes the purpose of that module. This comment does not
need to go into the entire API for the module, it should instead summarize the purpose and perhaps elaborate a bit on places
where this module might be useful.
Above every method
This is the most important kind of commenting, because it serves as documentation for your class's API.
Above each method there should be a very detailed comment describing everything someone would need to know
if they were to call the method. You must talk about any pre-conditions (what must a programmer ensure is true before this
method is called?); describe what range is permissible for parameters and what the purpose of the parameters is; desribe carefully
any "side-effects" that will occur while the method runs (What will the method DO? Will it print something on the console, change
the appearance of a widget in a GUI, modify a file, etc.); describe carefully how the return value (if any) is calculated.
Beside many variable declarations
As mentioned above, you should be choosing identifiers that are meaningful, so that it will be easier for the reader to understand
the role of the variable, in context. But many times it is also useful to describe (using a comment just after the variable's
declaration) the way in which a variable will be used.
Scattered throughout your methods
Any time there is a fragment of code that is not completely obvious, it is a good idea to provide comment(s) that describe how the
code works. Imagine that your friend is looking at your code and you are standing beside them explaining how the code works. Anything
you are likely to say in person should probably be included in the code as a comment. We want to guide the reader so that they can
understand the code more readily.