Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

All committers are expected to follow these standards; Checkstyle or similar (Lynt) is used to check compliance.

CDAP 6.9+

CDAP starting from version 6.8 uses slightly relaxed Google Coding Standards.

CDAP 6.8 and before

Summary

The main things for layout purposes in the standard are:

...

If you are not absolutely certain that the Thread using the ThreadLocal is short-lived, you must be sure to remove the ThreadLocal once it is no longer needed. Much like closing an InputStream in a finally block, classes that use a ThreadLocal must have a way to release all their resources. In the example below, a close() method is added to the ExampleClass that removes the ThreadLocal. After this, we can see that the Map in the ThreadLocal no longer has the objects.

Another option if possible is to make the ThreadLocal static so that there is only instance per thread.

Exceptions

This section gives general guidance on the use of exceptions when programming in Java.

  • try/catch blocks should be laid out like any other compound statement [mandatory]. For example:
    try {
        String str = someStrings[specifiedIndex];
    } catch (IndexOutOfBoundsException ex) {
        // The user specified an incorrect index, better take
        // some remedial action.
    } 
    

    When an exception is caught but ignored then a comment should be supplied explaining the rationale [mandatory]. For example:

    try {
        propertySet.setProperty("thingy", new Integer(10));
    } catch (UnknownPropertyException ignore) {
        // This exception will never occur as "thingy" definitely exists
    } 
     

  • All exceptions that are likely to be thrown by a method should be documented, except if they are runtime exceptions (note: the compiler will not enforce catch blocks for runtimes even if they are mentioned in the throws clause) [mandatory]. For example:

    /* Comment snippet:
     * @exception IllegalValueException Thrown if values is null or 
     *     any of the integers it contains is null.
     */
    private Integer sum(Integer[] values) throws IllegalValueException
  • InterruptedException should be caught and should not be ignored. It should set the correct state of the thread or status is checked before rethrowing.