Java Coding Guidelines
Java Coding Guidelines
Coding Guidelines
Version 1.3.2
All code must follow best practices. Part (but not all) of this is adhering to the following guidelines:
Development
For code development, I recommend the following these steps strictly in this order. Make sure to complete each step
fully before continuing with the next step.
1. Design your approach on paper, including class structure/relationships.
2. Implement the skeleton of your class structure/relationships, including all methods, major attributes, etc. Your
methods should already have JavaDoc, your attributes should already have comments, etc. Do not add any
implementation to the methods. Your code should compile. (e.g., if a method returns an object, simply return null to
make the code compile).
3. Fully implement testing based on the specification. For each element of the specification (e.g., throws exception if
name is null), write a test. Your test should be complete and compilable/executable. Of course, they will mostly fail
because your classes have no implementation.
4. Inside the various unimplemented methods, add comments for the implementation you plan to do.
5. Fill in the implementation between your comments.
6. Run tests and fix broken code.
Commenting
1. Add the following to the beginning of all of your source files:
/************************************************
*
* Author: <Your name>
* Assignment: <Assignment name>
* Class: <CSI class name>
*
************************************************/
To be clear, this is not the JavaDoc for the class
2. Add JavaDoc to all of your code, including classes, methods, etc. Before submission make sure that you can run the
JavaDoc export without any errors or warnings.
/**
* Adds two ints (a+b) and returns the result
*
* @param a first integer to add
* @param b second integer to add
*
* @returns the sum of a+b
* @throws OverflowException
* if a+b exceeds the value representable by int
*/
public int add(final int a, final int b) {
Note that JavaDoc follows a specific form:
- There are no dashes (-) between parameter and definition
- Use @ to define values like @author, @param, etc.
- Do not add extraneous information to method JavaDoc like method name.
3. Individually and meaningfully comment member variables and class constants.
4. Obvious/obfuscated comments are useless. Do not use them.
5. Properly (but reasonably) comment your code. A developer should be able to get a general idea of what’s going on
by just reading comments (and no code).
6. Each element needs a definition. This includes @param
@throws SpecialException // Bad!
@throws SpecialException if val is null or fails validation // Good!
7. Check your comments for spelling and grammatical errors.
Coding
1. You may only use the standard Java library and JUnit unless otherwise notified. Do not include any additional JARs in
your build path (and do not allow your IDE to automatically do so).
2. Follow the Java Coding Conventions from inventors of Java (see class page for document).
3. Do not use tabs.
4. Always use braces for code blocks, even for a single line of code. For example,
if (x == 5) {
System.out.println(“True!”);
}
The same rule applies for while, for, etc.
5. Import only necessary classes. Do not use wildcard imports (e.g., java.util.*) unless there are 4 or more classes from
that package.
6. Add any and all annotation hints to your code (e.g., @Override).
7. Eliminate code replication.
8. Eliminate code replication.
9. Seriously, eliminate code replication.
10. Properly address all compiler warnings. Do not suppress compiler warnings unless well justified. Include your
justification in a comment.
11. No spurious object creation.