Java Coding Guidelines

1. Base Coding Conventions

Sun Coding Conventions for the Java Programming Language:

http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html

2. Additional Guidelines

2.1 Imports

All imports must be single class and explicit. I.e. import <package>.* is not allowed.

2.2 Indentation

All indentation levels should be 4 spaces. No editor tabs are allowed unless they are converted to 4 spaces before saving the file.

2.3 Brackets

Two models are allowed (they must never be mixed within the same source file though, and should not be mixed within the same package):

1) Curly brackets ‘{}’are put on separate lines. E.g.

for (index = 0; index < length; index++)
{
    <code>
}

2) As defined in the Java Coding guidelines. E.g.

for (index = 0; index < length; index++) {
    <code>
}

2.4 Variables

No acronyms or abbreviations should be used. E.g. ‘a = b + mVarLen’ should be avoided and instead use: ‘totalLength = partLength + newLength’.

2.5 Instance Variables

Use ‘this.’ as prefix when referencing instance variables, e.g.:

public MyClass (ServicePropertiesInterface properties) {
    this.properties = properties;
}

public int foo () {
    int localInt = 3;
    return this.instanceInt + localInt;
}

2.6 One-Liners

Even single line statements should be inside brackets. E.g.

if (isEmpty) {
    return;
}

2.7 Logging

The Jakarta Commons API should be used exclusively. Log4j.properties should be used for configuring logs. System.out/err.println is not allowed.

2.8 Testing

Each component/class should have a JUnit test The tests should be put in test/ directory under each package directory.

2.9 Internationalization

2.9.1 What

2.9.2. How

The Java I18n/L10n Toolkit may be used to verify whether code is international.

2.10 Library Reuse

Treat all code as a library, and as a reusable component. Calls to System.exit() are disallowed (except the main method)

2.11 Exceptions

For local exceptions inherit from org.globus.ogsa.GridServiceException. If the exceptions should be exposed to remote clients define it in WSDL and extend the GridServiceFault defined in schema/core/faults/grid_service_fault.wsdl. Even though your wsdl operations don't throw any interface specific exception declare them to throw GridServiceFault.

2.12 IPv6 Considerations

Do not use local loop-back address "127.0.0.1". Use "localhost" instead or better yet use InetAddress.getLocalHost() to obtain the local ip address.
Do not create URL strings by hand. Use the URL class instead, preferably with URL(protocol, host, port, path) constructors. The URL(spec) constructor can also be used if the spec string can be specified in the configuration or such.

2.13 Proprietary API

Avoid using proprietary or implementation-specific API. Use the standard API as much as possible. If it is not possible to use the standard API try to abstract the proprietary calls into a separate calls so that it can be later replaced with another implementation.

For example:

2.14 Try/Catch/Finally

Whenever dealing with external resources such as files, network or database connections, etc. always make sure to release the resource after you are done with it. The simplest way to do that is to put the code that releases the resource in the finally clause of the try { .. } catch { .. } statement. The finally clause is always executed in any condition. For example:

 FileInputStream in = null;
 try {
    in = new FileInputStream("foo");
    in.read();
    ...
 } catch (IOException e) {
    System.err.println(e.getMessage());
 } finally {
   // will always be executed
   if (in != null) {
      in.close();
   }
 }