Java Coding Conventions

© 2005, Martin Rinehart
from www.MartinRinehart.com


Table of Contents

Introduction
Why So Many Conventions?
References to Conventions
Comment Types
Organization
 
1 General
1.1 Conventions About Conventions
1.2 Language
1.3 Implementation Comments
1.4 Acronyms
1.5 Other
 
2 Package
2.1 Package Names
2.2 Package Comments
 
3 Compilation Unit
3.1 Compilation Unit Names
3.2 Compilation Unit Comments
3.3 Compilation Unit Format
 3.3 Compilation Unit Format (cont.)
    3.3.1 Compilation Unit Sections
    3.3.2 Package and Import Statements
    3.3.3 Line Length and Breaks
    3.3.4 Indentation
3.4 Compilation Unit Other
 
4 Type (Class and Interface)
4.1 Type Names
4.2 Type Comments
4.3 Type Format
    4.3.1 Format of the Type Statement
    4.3.2 Items Within the Type
    4.3.3 Order of Methods
4.4 Type Other
 
5 Class-Wide Data
5.1 Class-Wide Data Names
5.2 Class-Wide Data Comments
5.3 Class-Wide Data Format
5.4 Class-Wide Data Other
6 Methods
6.1 Method Names
6.2 Method Comments
6.3 Method Format
6.4 Method Other
 
7 Statements Within Methods
7.1 Variable Names
7.2 Statement Comments
7.3 Statement Formats
    7.3.1 Agreed Statement Conventions
    7.3.2 Formatting the Ternary Expression
    7.3.3 Declarations Formats
    7.3.4 Block Statement Formats
    7.3.5 Switch Formats
    7.3.6 Try/Catch/Finally Formats
    7.3.7 Miscellaneous Formatting
7.4 Statement Other
 
Appendix — Convention Coverage Map


Introduction

Contents
1 General
2 Package
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
7 Statements Within Methods
Conventions Map
Sun has provided coding conventions for Java programmers (reproduced here at Code Conventions for the Java™ Programming Language and available at Sun). It would seem that an organization interested in coding conventions should adopt these and add whatever extensions are appropriate to its unique needs. It would be nice if life were so simple.

Why So Many Other Conventions?

Given the existence of Sun's conventions, why has an extensive crop of other conventions grown in the Java community? There are two main reasons: first, Sun's conventions are far from exhaustive and second, in some cases the other standards just disagree with Sun. In this article I'll cover the Sun conventions and four other thoughtful conventions. We'll see where they agree, where they fill in each others' gaps and where they disagree.

In alphabetical order, the non-Sun conventions are:

References to Conventions

Sun's document is reproduced here with fresh HTML so you can link directly to, for example, (6 Declarations — 6.1 Number Per Line) [Sun 6.1].

Ambler numbers sections (6 Classes, Interfaces, ... — 6.1 Classes — 6.1.1 Class Visibility). A reference to class visibility in Ambler is [Ambler 6.1.1].

Caltech does not provide a comparable navigation feature but it does provide a convenient, hyper-linked table of contents at the top. The reference to a naming convention will be [Caltech Naming Conventions].

Geosoft's organization is similar to Sun and Ambler (6 Layout and Comments — 6.1 Layout) but it numbers each convention consecutively. The first convention under 6.1 is "58 Basic indentation ...". This will be referred to as [Geosoft 58].

The New England Java User Group proposes Standards (must be followed — STD), Styles (should be followed, but some room for decision — STY) and Conventions (no recommendation, but make up your own mind and stick to your decision — CON). Style 13, Ternary Statement Usage, will be referred to as [NEJUG STY-13].

I have tried to be complete. For example, 2.2 Package Comments includes references to Ambler and to Caltech. This implies that the other three conventions do not address this issue.

Comment Types

In this document comments delimited by /* ... */ are called "block comments" while comments that run from their start to the end of their line // ... are called EOL comments. Some writers call block comments "C-style" comments and the EOL comment is called a "C++-style" comment. A comment block — multiple lines of comment in the code — may be enclosed in a block comment or each line may be an EOL comment.

Organization

The logic underlying Sun's organization was not apparent to this author. The other conventions attempt to improve on this with varying degrees of success. Here I'm going back to the basic organization of java itself:
  1. General
  2. Package
  3. Compilation Unit
  4. Type (Class and Interface)
  5. Class-Wide Data
  6. Methods
  7. Statements Within Methods
The General category covers items applicable to most or all of the other topics. For example, what language should we use for names and comments?

This paper doesn't cover "good advice" conventions that programmers using any language should know (choose meaningful variable names, for example). This paper also excludes requirements (the compiler reads files with a ".java" suffix). It focuses on choices the programmer needs to make. This document is not a substitute for the referenced documents — they include many useful examples and discussions of the options. The goal here is a terse summary that lets you see where the standards agree, disagree and are silent.

Contents
1 General
1.1 Conventions About Conventions
1.2 Language
1.3 Implementation Comments
1.4 Acronyms
1.5 Other
2 Package
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
7 Statements Within Methods
Conventions Map

1 General

  1.1 Conventions About Conventions

[Ambler 1.2] and [Geosoft 1] agree that violation of conventions is acceptable if the violation improves readability. Ambler insists that any violation must be documented.

Ambler's Law ([Ambler 1.5]) states that conventions are preferred in order of increasing generality. Industry conventions are better than organization conventions; organization conventions are better than project conventions, which in turn are better than personal conventions, which are better than no conventions.

  1.2 Language

What is the appropriate language for names and comments? Geosoft (a Norwegian company) specifies that both should be written in English [Geosoft 10, 77] as English is the international programming language. [Ambler 1.4] specifies the use of the writer's native language for names. (The other three conventions would have been noted here if they were not silent on this point.)

  1.3 Implementation Comments

Comments are divided into documentation (javadoc) and implementation (how and why) comments. [NEJUG STD-7] declares implementation comments are required. [Sun 5.1] encourages the use of implementation comments throughout the code. [Sun 5] and [Ambler 1.4] agree that comments should not be enclosed in banners.

  1.4 Acronyms

For names written in UpperAndLowercase (types), or lowerAndUppercase (methods and variables), acronyms should be capitalized as if they were words (call a sqlMethod(), create anHtmlPage) according to [Ambler 1.3] and [NEJUG STD-2].

  1.5 Other

Other NEJUG general conventions include being consistent within a file (if the style isn't written per your conventions, either change it all or make changes in the file conforming to the file's conventions) — [NEJUG STD-8]. Prefixes indicating variable scope are suggested as an option in [NEJUG STY-27]. Methods and classes that are just roughed in for future use are discouraged by [NEJUG CON-10]. The Error class should not be subclassed ([NEJUG CON-19]); checked and unchecked Exceptions should be distinguished ([NEJUG CON-20]) and Exceptions thrown should be enhanced with additional data ([NEJUG CON-21]).

[Caltech Structure] recommends that javadoc comments end with **/. The other standards do not specifically address this but all their examples show javadoc ending with */.

No convention specifies that javadoc comments have empty top and bottom lines, but all examples are done this way:


    /**
     * doc comment here
     */

Contents
1 General
2 Package
2.1 Package Names
2.2 Package Comments
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
7 Statements Within Methods
Conventions Map

2 Package

  2.1 Package Names

Except Ambler, all conventions ([Sun 9.1], [Caltech Naming Conventions], [Geosoft 2], [NEJUG STD-1]) specify that package names will be all lowercase. [Sun 3.1.2] specifies a reversed order URL (my packages would be com.martinrinehart.xxx) which guarantees uniqueness but may be cumbersome and certainly can't be followed by those who do not have a site of their own. [Caltech Structure, Naming Conventions] and [NEJUG STD-1] fully support Sun. [Ambler 6.3.1] and [Geosoft 2] do not require use of a reversed URL for packages not intended for distribution to others. Ambler and NEJUG point out that "java" and "javax" are reserved by Sun. Ambler states that package names should be singular, not plural.

  2.2 Package Comments

[Ambler 6.3.2] specifies that an external document — named "[package.name].html" — provide the rationale for the package and a list of classes. [Caltech Structure] requires package documentation in a file named "package.html". Ambler argues that multiple files with the same name, even though in different directories, will eventually be a source of trouble. Caltech's recommendation takes advantage of the Javadoc tool's ability to incorporate "package.html" into the API documentation. Javadoc includes a list of classes taken from the source files in the package directory.

Contents
1 General
2 Package
3 Compilation Unit
3.1 Compilation Unit Names
3.2 Compilation Unit Comments
3.3 Compilation Unit Format
    3.3.1 Compilation Unit Sections
    3.3.2 Package and Import Statements
    3.3.3 Line Length and Breaks
    3.3.4 Indentation
3.4 Compilation Unit Other
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
7 Statements Within Methods
Conventions Map

3 Compilation Unit

  3.1 Compilation Unit Names

(Requirements enforced by the compiler — name the unit the same as the name of the public type with a ".java" suffix — are not considered conventions.)

  3.2 Compilation Unit Comments

[Sun 3.1.1] specifies that the file (more correctly, compilation unit, but to date remains commonly a single file) begin with a block comment specifying the classname, version, date and copyright notice. [Caltech Structure] similarly specifies a block comment specifying project, copyright, version control ID (for its version-control system), list of classes (if more than one) and principal entry point, if any. [Ambler 6.4.2] also includes an optional file name, copyright notice and list of classes.

[Sun 3] also suggests an optional comment identifying the sections of the file.

Embedded HTML tags in comments are specified by [Caltech Documentation] in an effort to make intra-file navigation as simple as Javadoc navigation. (Caltech does not claim to have solved the problem, only to be working on a solution.)

  3.3 Compilation Unit Format

There is general agreement that less is more when it comes to the types in a single compilation unit. [Sun 3.1] specifies a single public type followed by zero or more private ones. [Caltech Structure] specifies one type per file except for pure "one-shot" (supporting the main type, never a candidate for independence) classes. Similarly, [Geosoft 30] allows just one type plus associated inner classes.

    3.3.1 Compilation Unit Sections

[Sun 3] specifies that the sections of the file are separated by blank lines; [Sun 8.1] specifies that exactly two blank lines should be used between the types. [NEJUG STY-28] concurs. By inference, separate types are "sections" of the compilation unit. It is not clear if divisions within a type (static variables, instance variables, constructors and other methods) constitute separate sections.

The order within a file is beginning comments, package and import statements, then class and interface declarations, per [Sun 3.1.1] and [NEJUG STY-1]. The public type comes before any others.

    3.3.2 Package and Import Statements

[Geosoft 35] specifies that the package statement must be present (forbidding the use of the default package).

The imports should use "*" (as in java.awt.*) according to [Ambler 7.2], and shouldn't use "*", according to [Ambler 7.2.1] and [Caltech Structure]. [NEJUG STY-3] considers both positions and concludes that "*" should be used for standard (Sun) classes but not for your own classes. The order of imports is java.xxx first, javax.xxx next and then others alphabetically according to [Caltech Structure] and [Geosoft 36]. [NEJUG STY-2] suggests that third-party packages be imported between Sun's and your own. [Sun 3.1.2] only says that imports follow the package statement.

    3.3.3 Line Length and Breaks

Line length should be limited to 80 characters according to [Sun 4.1], [Geosoft 32] and [NEJUG STY-6]. [Caltech Structure] specifies 78 characters. Sun specifies 70 characters for documentation.

Over-length lines must be broken. The broken part is indented ([Sun 4.1], [Ambler 2.4.3], [Geosoft 34] and [NEJUG STY-8]). Breaks are after commas and operators (except Sun, which specifies breaking before binary operators). Geosoft and NEJUG strongly disagree, stating that the "broken" condition of the line should be made as obvious as possible — which means the break is after the operator.

    3.3.4 Indentation

While the need for indenting is universally noted, the exact nature of indenting is: The argument against tabs is that if your tab stops are set differently than mine, we won't see the source code the same way.

  3.4 Compilation Unit Other

[Sun 3] and [NEJUG CON-5] say that the file should not exceed 2,000 lines.

Contents
1 General
2 Package
3 Compilation Unit
4 Type (Class and Interface)
4.1 Type Names
4.2 Type Comments
4.3 Type Format
    4.3.1 Format of the Type Statement
    4.3.2 Items Within the Type
    4.3.3 Order of Methods
4.4 Type Other
5 Class-Wide Data
6 Methods
7 Statements Within Methods
Conventions Map

4 Type (Class and Interface)

  4.1 Type Names

Type names should be written using the UpperAndLower convention ([Sun 9.2], [Ambler 1.3, 6.1.2], [Geosoft 3], and [NEJUG STD-2]). Class names should be nouns ([Sun 9.2], [Ambler 6.1.2], [Caltech Naming Conventions] and [NEJUG STD-2]).According to [Caltech Naming Conventions] interface names should end with "able" or the word "Interface". According to [Ambler 6.2.1] interface names should be adjectives, like Runnable, or nouns like DataInput.

[Caltech Naming Conventions] also suggests starting with "Abstract" and ending with "Factory" and "Exception" for the respective class types, and ending with "Impl" for an implementation. [Geosoft 27, 28] specifies starting with "Default" and ending with "Exception" for those types.

[Ambler 1.3] recommends against names longer than 15 characters.

  4.2 Type Comments

A javadoc comment precedes the type statement ([Sun 5.2], [Ambler 1.4.1], [Caltech Structure], [Geosoft 37], [NEJUG STD-6]), According to [Caltech Structure] this should include one @version and at least one @author tag. According to [Ambler 6.1.3] the javadoc comment should include purpose, known bugs, code history, any invariants and concurency issues. For an interface, [Ambler 6.2.2] specifies that the comment include how to use and how not to use the interface.

An implementation comment should follow the type statement, per [Sun 5.2].

// end of class Xxx may follow the closing brace, per [Caltech Structure].

  4.3 Type Format

    4.3.1 Format of the Type Statement

Only [Geosoft 60] specifies the format of the type statement:

type name extends superclassname
  implements interface list

    4.3.2 Items Within the Type

Within the type, the order is:
  1. javadoc comment
  2. class or interface statement
  3. type implementation block comment if necessary
  4. static variables (public, then protected, then package, then private)
  5. instance variables (same order)
  6. constructors
  7. other methods
This is agreed by [Sun 3.1.3], [Geosoft 37] and [NEJUG STY-4,5].

No convention mentions the main() method. This is a problem for me as I almost always write a main() for testing and comment it out after testing is completed. Once commented out, it is invisible to my IDE so I cannot navigate to it — or I couldn't if I didn't know that it immediately precedes the first constructor.

    4.3.3 Order of Methods

The order for methods is definitely not agreed. It is one of: After deciding on an order, then you can decide to separate your methods one of these ways: The open brace is on the same line as the type statement [Sun 6.4], or it is on the next line [Geosoft 59]. The close brace is not indented, on a line by itself [Sun 6.4].

  4.4 Type Other

"Facade" classes should be public, others should use package visibility per [Ambler 6.1.1]. Use of public and protected visibility should be minimized, per [Ambler 6.1.5].

Contents
1 General
2 Package
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
5.1 Class-Wide Data Names
5.2 Class-Wide Data Comments
5.3 Class-Wide Data Format
5.4 Class-Wide Data Other
6 Methods
7 Statements Within Methods
Conventions Map

5 Class-Wide Data

Note: More precisely, but possibly confusingly, these conventions are type-wide — class and interface — not just class-wide.

  5.1 Class-Wide Data Names

Use lowerAndUpper convention [Sun 9.5], [Ambler 1.3, 3.1.1], [Caltech Naming Conventions], [Geosoft 4, 11], [NEJUG STD-3]. Use UPPER_AND_UNDERSCORE convention for constants. [Sun 9.6], Ambler 3.1.3], [Caltech Naming Conventions], [Geosoft 5], [NEJUG STD-5].

Avoid long names (over 15 chars) and names differing only in case [Ambler 1.3].

Name widgets with a postfix type: okButton, nameField ([Ambler 3.1.2] and [Geosoft 18]).

Collections: use plural noun: customers, products ([Ambler 3.1.4] and [Geosoft 19]).

Private class variables should have an underscore suffix (name_) [Geosoft 8]. Generic variables should have the same name as their type (Database database) [Geosoft 9]. Use an "n" prefix for number of (nLines) [Geosoft 20]. Use a "No" suffix for entity number (employeeNo) [Geosoft 21]. Use complement names (first/last, next/previous) [Geosoft 23]. Don't abbreviate (ActionEvent event) [Geosoft 24]. Associated constants should share a prefix (COLOR_RED, COLOR_BLUE) [Geosoft 26].

  5.2 Class-Wide Data Comments

Provide javadoc ([Sun 5.2], [Ambler 1.4.1], [Caltech Structure]). Document: description, invariants, examples, concurrency, visibility (if not private) [Ambler 3.3].

Collections of a single type should have an EOL comment (ArrayList coins; // of Coin) [Geosoft 80].

  5.3 Class-Wide Data Format

One declaration per line [Sun 6.1]. All data before methods, statics before (or after) instance [Caltech Structure], or variables and methods together in order of visibility (public, protected, package, private) [Ambler 6.1.4.2].

Offset groups with comments [Caltech Structure].

Left-align variable names in declarations ([Sun 6.1], [Geosoft 74]). Example:


    Widget[]    widgets;
    int         nWidgets;

  5.4 Class-Wide Data Other

Per [Sun 10.1] and [Geosoft 42], don't use "public" access unless you really have a struct (a class with fields but no methods manipulating those fields). More strictly, use minimal visibility per [Ambler 2.2] and [NEJUG CON-7], which means all fields should be private, accessed through getters/setters per [Ambler 3.2, 3.4] and [NEJUG CON-8].

Initialize fields in declarations or in a constructor, but not both ([NEJUG STY-17]). Define constants in an interface ([NEJUG CON-13]).

Contents
1 General
2 Package
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
6.1 Method Names
6.2 Method Comments
6.3 Method Format
6.4 Method Other
7 Statements Within Methods
Conventions Map

6 Methods

  6.1 Method Names

Method names should be verbs ([Sun 9.4], [Ambler 2.1], [Geosoft 6], [NEJUG STD-3]) and they should use the lowerAndUpper convention [Sun 9.4], [Ambler 1.3, 2.1], [Caltech Naming Conventions], [Geosoft 6], [NEJUG STD-3]. Getters and setters should use bean convention names — getXxx(), isXxx() and setXxx() — per [Ambler 2.1.1, 3.4.1], [Caltech Naming Conventions], [Geosoft 13], [NEJUG STY-26].

Converter methods start with "to" (toDegrees()) [Caltech Naming Conventions].

Geosoft has much more to say about method names. The name of the object should not be included (line.getLength(), not line.getLineLength()) [Geosoft 12]. If the method computes something, use "compute" (computeAverage() ) [Geosoft 15]. Use "find" for lookup (findNearestVertex() ) [Geosoft 16]. Use "initialize" (printer.initializeFontSet() ) [Geosoft 17]. Use complement names (get/set, add/remove) [Geosoft 23]. Don't abbreviate (computeAverage()) [Geosoft 24]. Don't negate booleans (isFound(), not isNotFound() ) [Geosoft 25]. Methods returning data should be named after what they return; void methods after what they do [Geosoft 29].

  6.2 Method Comments

Provide all public methods with javadoc [Sun 5.2], [Ambler 1.4.1], [Caltech Structure], [NEJUG STD-6]. All public and protected methods need javadoc [Geosoft 81]. The method comment should cover what/why method does, parameters, returns, bugs, exceptions, visibility decisions, changes to object, code change history, examples, pre/post conditions and concurrency [Ambler 2.3.3].

Implementation comments should precede methods [Sun 5.1]. Implementation comments should explain what and why [NEJUG CON-11]. Implementation comments cover control structure, what/why, local variables, difficult or complex code, and processing order. [Ambler 2.3.3].

For non-trivial methods, document the closing brace [Ambler 2.3.3]:


public type whateverMethod(parameters) {
...
... non-trivial code here
...
} // end of whateverMethod()
In Javadoc, parameters should have purpose, restrictions/preconditions, and examples, if required [Ambler 5.2].

  6.3 Method Format

Use one blank line between the javadoc comment and the method's code, per [Caltech Structure]. Others do not show this blank line in their examples.

[Geosoft 38] Specifies the order for the modifiers to be access, static, abstract, synchronized, , final, native.

It is agreed that no space separates the name and open parenthesis [Sun 6.4], [NEJUG STD-3].

The parameters are all on one line, or each can be on its own line, per [Caltech Structure].

There is no agreement on the location of the open brace. It goes on the same line as the name [Sun 6.4] or on the next line [Caltech Structure], [Geosoft 59].

Both styles place the close brace on a line by itself, indented to the start of the name line — [Sun 6.4], [Caltech Structure].

[Sun 6.4] specifies "{}" for an empty method.

[Geosoft 61] specifies this method format:


public void someMethod()
  throws SomeException
{
...
}

  6.4 Method Other

Access static methods by ClassName.method(), not by an instance variable ([Sun 10.2], [Geosoft 57] and [NEJUG CON-15].

Use minimal visibility [Ambler 2.2]. Getters/setters should be protected, if possible [Ambler 3.4.3].

Constructors should leave objects in a stable state [NEJUG CON-2]. Methods should accomplish a single task [NEJUG CON-3].

In a group of size recommendations, NEJUG stresses that the exact number should be set to meet the organization's needs. Limit methods to 30 (or other) lines [NEJUG CON-4]. Limit number of methods to 30 (or other) [NEJUG CON-6]. Limit number of parameters to 5 (or other) [NEJUG CON-9]. Minimize public methods and variables [NEJUG CON-7].

Contents
1 General
2 Package
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
7 Statements Within Methods
7.1 Variable Names
7.2 Statement Comments
7.3 Statement Formats
    7.3.1 Agreed Statement Conventions
    7.3.2 Formatting the Ternary Expression
    7.3.3 Declarations Formats
    7.3.4 Block Statement Formats
    7.3.5 Switch Formats
    7.3.6 Try/Catch/Finally Formats
    7.3.7 Miscellaneous Formatting
7.4 Statement Other
Conventions Map

7 Statements Within Methods

  7.1 Variable Names

There are few disagreements about variable names. They should use the lowerAndUppercase convention ([Sun 9.5], [Ambler 1.3, 3.1.1], [Geosoft 4, 11], [NEJUG STD-4]) and a lower-level name (in a code block) should not hide a higer-level (outer block or class-wide) name ([Sun 6.1], [Ambler 3.1.5], [NEJUG STD-9]).

Single-letter names are OK for "throwaway" variables (very limited scope) such as loop counters [Sun 9.5], [Ambler 4.1.2], [Geosoft 11]. Just "e" is OK for an Exception [Ambler 4.1.3] and "i", "j", "k" can name iterators (Iterator i = ...) [Geosoft 22].

[Ambler 4.1.1] suggests xxxStream for streams and later, [Ambler 5.1] suggests using the same name conventions for parameters as for other variables.

[Geosoft 24] warns us not to abbreviate (ActionEvent event).

  7.2 Statement Comments

Most conventions stress good coding practices — if your code needs extensive comments considering re-writing as an alternative to commenting. Of course, comments are desired and sometimes required, The non-disputed in-method conventions are After these, there is considerable disagreement on the subject of block comments and EOL comments. Some conventions avoid the use of block comments for commentary, as they make it harder to use block comments to comment out code. Others use EOL comments to comment out code as they make the commented-out nature stand out. These are the positions: Finally, there are these additional conventions. Use XXX in a comment if the code is bogus, but it works; use FIXME if the code is bogus and broken [Sun 10.5.4]. You should document closing braces in long blocks ([Caltech Structure] and [NEJUG CON-17]) and you should comment variable declarations, including the addition of new variables, per [NEJUG STY-21].

  7.3 Statement Formats

    7.3.1 Agreed Statement Conventions

A few conventions are generally agreed. There should be just one statement per line ([Sun 7.1], [Ambler 2.4.6, 4.2], [NEJUG STY-21, 23]). The one disagreement comes from [Geosoft 43] which allows related variables of a single type to be declared together: (float x, y, z;). The use of assignment within another expression is discouraged by [Sun 10.4] and [Geosoft 53]

Blank lines should be used to "paragraph" — group related lines together — the code ([Sun 8.1], [Ambler 2.4.2, 2.4.4], [Geosoft 72], [NEJUG STY-28]). Similarly, a blank line can separate variable declarations from the start of the code ([Sun 8.1] and [NEJUG STY-28]).

The standards are nearly unanimous in stressing the use of parentheses as superior to depending on precedence rules ([Sun 10.5.1], [Ambler 2.4.7], [Caltech Structure], [NEJUG CON-16]).

We should use a single space:

    7.3.2 Formatting the Ternary Expression

[Sun 4.2] suggest three ways to format the ternary:

    1 a = (expr) ? b : c;
    2 a = (expr) ? b
                 : c;
    3 a = (expr)
          ? b
          : c;
Only the first of these meets Geosoft's and NEJUG's concern for having the "brokenness" of the line be obvious (see 3.3.3 above re breaking lines in compilation units). Additionally [NEJUG STY-13] suggests that we use only simple, never nested, ternary expressions.

[Sun 10.5.3] specifies that the ternary's condition is always parenthesized.

    7.3.3 Declarations Formats

All declarations should be at the beginning of blocks (or, for loop control variables, in "for" statements) if you agree with [Sun 6.3] and [NEJUG STY-22] or the declarations should be immediately before the variables' use, if you agree with [Ambler 4.2]. Possibly splitting the difference, [Geosoft 40, 45] tells us to declare local variables in the smallest possible scope.

[Geosoft 47] tells us to initialize loop variables just before the loop:


    boolean isDone=false;
    while (!isDone)
    {
    ...
    }
Similarly [Geosoft 46] tells us to never include initialization or update for anything except the loop control variables in a "for" statement. Other initialization should be done prior to the "for" and updates should be in the block statement body.

[Sun 6.2] and [Geosoft 40] specify initialization of local variables where declared (if possible). [NEJUG STY-16] agrees but only if the initial values are not the default values as this adds to the code size and is redundant. Sun's example declarations show ints being initialized to zero.

    7.3.4 Block Statement Formats

The same dispute about the placement of opening braces exists within the method as it does for the method itself. This is the style from [Sun 7.2]:

    if (expr) {
        statement[s];
    }
This is the preference of [Geosoft 59]:

    if (expr)
    {
        statement[s];
    }
This is the standard from [Sun 7.4]:

    if (expr) {
        statement[s]
    } else {
        statement[s]
    }
[Geosoft 62] says we should always use a new line for each "else."

We should always use braces [Sun 7.2], [Caltech Structure], [NEJUG STY-24] or we can skip them for a single-line statement [Geosoft 69]. If you use the single-line statement, it should be on a separate line, per [Geosoft 52].


    if (expr)
        statement;
In [NEJUG STY-11] we are asked to indent controlled statement blocks (if, for, while, etc.) one level. [NEJUG STY-12] declares neutrality on the opening brace same line or next line debate.

If necessary to break a conditional, indent the broken part(s) 8 spaces and the following block statement just 4 spaces ([Sun 4.2]).

An empty block after a "for" statement should be a semi-colon on the same line as the "for" [Sun 7.5] or it should be a semi-colon placed on the following line [Geosoft 63, 64]. This also applies to "while" loops ([Sun 7.5]).

    7.3.5 Switch Formats

Per [Sun 7.8] the general form of a switch statement is:

    switch (condition) {
        case A:
            statements;
            break;
        case B:
            statements;
            break;
        default:
            statements;
            break;
    }
If you wish execution to fall through from case A into case B, replace the break with "/* falls through*/" ([Sun 7.8]) or with "// falls through" ([Geosoft 67]) or never fall through — always include the "break" ([NEJUG STY-14]). [NEJUG STY-15] requests that the "default" case always be included.

Additionally [Geosoft 70] specifies a single space before the colon (case A :).

    7.3.6 Try/Catch/Finally Formats

[Sun 7.9] specifies this format:

    try {
        statements;
    } catch (ExceptionClass e) {
        statements;
    } finally {
        statements;
    }
As always, [Geosoft 68] leaves the closing braces on lines of their own.

    7.3.7 Miscellaneous Formatting

[Ambler 2.5.2] suggests putting constants on left side of comparisons: if ( 1 < x ) ... .

[Geosoft 44] declares arrays with brackets next to type(int[] foo, not int foo[]).

[Geosoft 75] asks for horizontal alignment to be used to enhance readability:


   aWidget.      addMouseListener();
   theWidget.    addMouseListener();
   anotherWidget.addMouseListener();

Contents
1 General
2 Package
3 Compilation Unit
4 Type (Class and Interface)
5 Class-Wide Data
6 Methods
7 Statements Within Methods
Conventions Map

  7.4 Statement Other

We should use named constants, not numeric constants except for 1, 0 and -1 in loop control, per [Sun 10.3] and [NEJUG STY-29]. [Geosoft 54] concurs but omits the "-1".

Return boolean (or ternary) rather than use multiple returns within an if/else construct ([Sun 10.5.2]).

Document local variables with an EOL comment ([Ambler 4.2]). Also, never reuse local variables ([Ambler 4.2] and [Geosoft 41]).

Additional conventions from Geosoft are:

Finally, [NEJUG CON-1] recommends protecting code with try/finally blocks and [NEJUG CON-12] recommends against nesting conditions more than four deep.


Appendix - Convention Coverage Map

TopicSunAmblerCaltechGeosoftNEJUG
1 General 
1.1 Conventions About Conventions 1.2, 1.5 1 
1.2 Language 1.4 10, 77 
1.3 Implementation Comments5, 5.11.4  STD-7
1.4 Acronyms 1.3  STD-2
1.5 Other  Structure STD-8, STY-27, CON-10, 19-21
2 Package 
2.1 Package Names3.1.2, 9.16.3.1Naming Conventions2STD 1
2.2 Package Comments 6.3.2Structure  
3 Compilation Unit 
3.1 Compilation Unit Names     
3.2 Compilation Unit Comments3, 3.1.16.4.2Documentation, Structure  
3.3 Compilation Unit Format3.1 Structure30 
    3.3.1 Compilation Unit Sections3, 3.1.1, 8.1   STY-1,28
    3.3.2 Package and Import Statements3.1.27.2, 7.2.1Structure36STY-2,3
    3.3.3 Line Length and Breaks4.12.4.3 34STY-8
    3.3.4 Indentation42.4.2Structure33, 58STY-9,10
3.4 Compilation Unit Other3   CON-5
4 Type (Class and Interface) 
4.1 Type Names9.21.3, 6.1.2, 6.2.1Naming Conventions3, 27-8STD-2
4.2 Type Comments5.21.4.1, 6.1.3, 6.2.2Structure37STD-6
4.3 Type Format 
    4.3.1 Format of the Type Statement   60 
    4.3.2 Items Within the Type3.1.3  37STY-4,5
    4.3.3 Order of Methods3.1.3, 6.4, 8.16.1.4.2Structure59, 73STY-5,28
4.4 Type Other 6.1.1, 6.1.5   
5 Class-Wide Data 
5.1 Class-Wide Data Names9.5, 9.61.3, 3.1.1-4Naming Conventions4, 5, 8-9, 11, 18-21, 23-4, 26STD-3,5
5.2 Class-Wide Data Comments5.21.4.1Structure80 
5.3 Class-Wide Data Format6.16.1.4.2Structure74 
5.4 Class-Wide Data Other10.12.2, 3.2, 3.4 42CON-7,8,13 STY-17
6 Methods 
6.1 Method Names9.41.3, 2.1, 2.1.1, 3.4.1Naming Conventions6, 12-3, 15-7, 23-5, 29STD-3, STY-26
6.2 Method Comments5.1-21.4.1, 2.3.3, 5.2Structure81STD-6, CON-11
6.3 Method Format6.4 Structure38, 59, 61STD-3
6.4 Method Other10.22.2, 3.4.3 57CON-2,4,6,7,9,15
7 Statements Within Methods 
7.1 Variable Names6.1, 9.51.3, 3.1.1, 3.1.5, 4.1.1-3, 5.1 4, 11, 22, 24STD-4,9
7.2 Statement Comments5.1.2-4, 8.1, 10.5.41.4.1Documentation, Structure78STY-18-21,28 CON-17
7.3 Statement Formats 
    7.3.1 Agreed Statement Conventions7.1, 8.1-2, 10.4, 10.5.12.4.2, 2.4.4, 2.4.6-7, 4.2Structure43, 53, 72STY-21,23,28 CON-14
    7.3.2 Formatting the Ternary Expression4.2, 10.5.3   STY-13
    7.3.3 Declarations Formats6.2-34.2 40, 45-7STY-16,22
    7.3.4 Block Statement Formats4.2, 7.2, 7.4-5 Structure52, 59, 62-4, 69STY-11-2,24
    7.3.5 Switch Formats7.8  67, 70STY-14-5
    7.3.6 Try/Catch/Finally Formats7.9  68 
    7.3.7 Miscellaneous Formatting 2.5.2 44, 75 
7.4 Statement Other10.3, 10.5.24.2 39, 41, 48-51, 54-6STY-29 CON-1,12


java consultant home page icon

Back to Articles