Course Coding Standards
This document consists of the java coding standards for the course. All project files must fully comply with these standards to be accepted for grading.
File Length
- Java files of more than 2,000 lines of code should be avoided. Use more classes and files to split up long files.
Java Source Files
- All Java source code files must have this general order. The details of each section are specified later in this document.
- Package statement
- Import statements
- Class and Interface declarations
Package Statements
- The first non-comment line of java source files is a
package
statement.
package java112.analyzer;
Import Statements
- After the
package
statement comes all import statements.
import java.io.*;
import java.util.*;
Class and Interface Declarations
- Class or Interface parts should appear in this order.
- Class or Interface javadoc comment
- The keyword
public
followed by theclass
orinterface
statement, including the name of the class or interface. - Constants.
- Class (
static
) variables. - Instance variables that are
protected
. - Instance variables that are
private
. - The empty class constructor.
- All other class constructors.
- Methods.
Indentation
- Four space characters (not a tab character) should be used for indentation of nested code. Projects will not be accepted if any of your java source code files contain tab characters.
- Most text editors allow you to set your editor such that a tab is 4 spaces. For example, in Atom: Edit >> Preferences >> Editor, 1) Check "Soft Tabs" 2) “Tab Length to 4. This is an acceptable practice.
Line Length
- All lines of java must be 80 characters or less if at all possible. This is possible almost all the time.
- All lines of javadoc or other comments must be 70 characters or less.
Line Wrapping
- When a line of code needs to span more than one line, you should use these general principles:
- Break after a comma.
- Break before an operator.
- Use higher-level breaks over lower-level breaks.
- Indent all continuation lines by 8 spaces or more for clarity. Making the java statement clear is the goal.
- Examples:
// Correct java code continuation
function(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5);
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6;
//INDENT 8 SPACES TO AVOID VERY DEEP INDENTS
private static synchronized extremelyLongMethodName(int anArg,
Object anotherArg, String yetAnotherArg,
Object andStillAnother) {
...
}
if ((condition1 && condition2)
|| (condition3 && condition4)
||!(condition5 && condition6)) {
doSomethingAboutIt();
}
Comments in Java Source Code
- Good
javadoc
comments are required before all:- class statements
- interface statements
- methods
Variable Declarations
- All variables and constants must be declared on their own line.
- Here is the required order for variables and constants.
- Constants
- Class variables
- Instance variables
- All local variable must be declared at the top of a method.
- Do not declare a local variable with the same name as a class or instance variable.
- Local variables must be initialized when they are declared.
Compound Statements
- Compound statements should look like the following examples. The left curly brace (
{
) should always be at the end of the beginning line of the statement.
// Compound statement examples
// if statements
if (condition) {
statements;
}
if (condition) {
statements;
} else {
statements;
}
if (condition) {
statements;
} else if (condition) {
statements;
} else if (condition) {
statements;
} else {
statements;
}
//for statements
for (initialization; condition; update) {
statements;
}
//while statements
while (condition) {
statements;
}
//try/catch statements
try {
statements;
} catch (ExceptionClass exception) {
statements;
}
- All
for
andwhile
loops must use curly braces.
Blank Spaces
- A java keyword that is followed by a set of parentheses must have a space between the keyword and the left parentheses.
- Within a set of parentheses, there should be no spaces after the left parentheses or before the right parentheses.
// Keyword spacing
while (true) {
...
}
- All commas in lists must be followed by a space.
// Commas and spaces
addThreeNumbers(5, 10, 15);
- All binary operators except
.
(the period) must have space before and after the operator.
// Operators and spaces
total = variableOne + variableTwo;
total = (variableOne + variableTwo) / (variableThree * variableFour);
Naming Conventions
- All class and interface names must start with an uppercase letter.
- All variable names must start with a lowercase letter.
- All method names must start with a lowercase letter.
- All method names should start with a verb and contain more than one word.
- No abbreviations are allowed in the names of classes, interfaces, variables, or methods. Use only full words for clarity.
- No one or two character variable names are allowed.
- Use the “camel case” convention for names that have more than one word. Each word in a name (except for the first word of variables and methods) will start with an uppercase letter. All other letters in the name will be lowercase.
- Constants must be all uppercase letters with a underscore separating them.
Variable Access
- All instance variables must be declared
private
. - All instance variables that need to be used outside of a class need to have accessor methods using correct naming conventions as shown in class.