Java Code Conventions

Please adhere as far as possible to Sun's Code Conventions. While these conventions are no longer updated, they are a good basis for writing comprehensible code.

The code conventions will be enforced using checkstyle.

A checkstyle file for the important rules below is available in the repository under key/scripts/tools/checkstyle/key_checks.xml.

The rule identifiers in parentheses refer to rules in this checkstyle file.

Here are adapted excerpts of the most important guidelines:

Code conventions are important to programmers for a number of reasons:

JavaDoc

Use JavaDoc to annotate at least: * every class, * every field (even if private), * and every public method that is not a setter or getter.

When you create a new class, add yourself as the author of the class (using @author). If you make a noteworthy contribution to a class, add yourself as a further author.

(JavadocMethod), (JavadocType), (JavadocVariable)

Indentation

Four spaces should be used as the unit of indentation. The exact construction of the indentation (spaces vs. tabs) is unspecified. Tabs must be set exactly every 8 spaces (not 4).

(FileTabCharacter), (Indentation)

Lines

Avoid lines longer than 100 characters.

(The conventions require that all lines are no longer than 80 characters. For KeY, we have relaxed this rule.)

Avoid trailing spaces in lines.

(RegexpSingleline-tr-spaces)

Compound Statements

Compound statements are statements that contain lists of statements enclosed in braces "{ statements }". See the following sections for examples.

(LeftCurly), (RightCurly), (NeedBraces)

if, if-else, if else-if else Statements

The if-else class of statements should have the following form:

 if (condition) {
     statements;
 }

 if (condition) {
     statements;
 } else {
     statements;
 }

 if (condition) {
     statements;
 } else if (condition) {
     statements;
 } else {
     statements;
 }

Note: if statements always use braces {}. Avoid the following error-prone form:

 if (condition) // AVOID! THIS OMITS THE BRACES {}!
     statement;

try-catch Statements

A try-catch statement should have the following format:

 try {
     statements;
 } catch (ExceptionClass e) {
     statements;
 }

Naming Conventions

Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.

Packages

Package names are always written in all-lowercase ASCII letters [...].

Examples: * com.sun.eng * com.apple.quicktime.v2 * edu.cmu.cs.bovik.cheese

(PackageName)

Classes / Interfaces / Enums

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

Examples * class Raster * class ImageSprite

(TypeName)

Methods

Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized.

Examples: * run(); * runFast(); * getBackground();

(MethodName)

Variables

Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should not start with underscore _ or dollar sign $ characters, even though both are allowed.

Variable names should be short yet meaningful. The choice of a variable name should be mnemonic, that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary "throwaway" variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

Examples: * int i; * char c; * float myWidth;

(LocalFinalVariableName), (LocalVariableName), (ParameterName)

Constants

The names of variables declared class constants (static final) should be all uppercase with words separated by underscores ("_").

Examples * static final int MIN_WIDTH = 4; * static final int MAX_WIDTH = 999; * static final int GET_THE_CPU = 1;

(ConstantName)

No prefixes

Be aware that names are not to be prefixed by the type of entity they are.

Declarations

(DeclarationOrder), (MultipleVariableDeclarations)

Statements

(MethodLength), (NoIncrementInExpr)