5.4. Statements

5.4.1. If/else

Braces must be used in the if/else blocks, even if there is a single statement. To illustrate:

if (true) {
    doThis();
}

The following is not allowed:

 if (true)
     doThis();

The position of the braces should be the same as in the first example. The following format is incorrect:

if (true)
{
    test1();
    test2();
}

5.4.2. Try/catch

All exceptions require a statement; no silent catching is allowed. For example:

try {
   doThis();
} catch (Exception e) {
    // should not occur
}

A logger can be used:

try {
    doThis();
} catch (Exception e) {
    logger.logDebug("Exception while doing .....", e);
}

5.4.3. Inline Conditionals

Inline conditionals are not allowed. The following code is incorrect:

b = isOk() ? true : false;

The correct way to write this is as follows:

if (isOk()) {
    b = true;
} else {
    b = false;
}

5.4.4. Naming Conventions

5.4.4.1. Static Final Attributes

Declarations are static final, not final static. This is a JLS recommendation.

5.4.4.2. Constants

Constants should be static and final, and should adhere to the following:

'^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$'

5.4.4.3. No Magic Numbers, Use Constants

Constants must be used in the code and magic number must be avoided. For example, the following is not allowed:

private int myAttribute = 5;

The correct format is:

/**
 * Default value
 */
private static final int DEFAULT_VALUE = 5;

/**
 * This attribute is initialized with the default value
 */
private int myAttribute = DEFAULT_VALUE;

5.4.4.4. Attribute Name

The attribute name should not have an underscore ( _ ). The _ is valid for constants that are in uppercase.

Use pValue and mValue instead of p_Value and m_Value.

The pattern for attribute name is:

'^[a-z][a-zA-Z0-9]*$'