Java coding guidelines

Specify a package name for your java classes. This typically looks lik

package com.ai.aspire;
public class SomeDataStructure
{
}

It has been a common practice to name your packages in lowercase. I haven't seen mixed case that much.

It is also common practice to have the classnames starting with an upper case and move to a mixed case then on. Example

String
Vector
List

It is very useful to say something about the class and what its role is. Unless we are naming experts it is hard to grasp what the class does solely based on its name. It is nice if it does. But we can't depend on it.

Example

package com.ai.aspire;
/**
* This class is responsible for something
* It does this. does this etc.
*/
public class SomeDataStructure {}

Method names start with a lowercase and then proceed to mixed case on a new word boundary. Again this is just a convention

When you are writing a method, sometimes you have the entire method body encapsulated in a single big try block. If the length of this block is more than 10 to 15 lines, think of writing a another function that does a throw of the most of the stuff and call that function through the main method. That way you have a place to deal with your errors unconfusing your main line logic.

Provide an example.

Although "temp", "i", "k" are all valid variables under some circumstances, make their names more meaningful if you can.

Try to keep your method body to with in 15 to 20 lines where applicable.

If your method has some exclusion conditions that you want to avoid before getting to the main part you can use the following pattern

if (cond1)
{
   do something and return;
}
if (cond2)
{
   do something and return;
}

//Here is the main logic
do something

This is easier to read than putting an if and else around the whole thing. It is difficult follow the chain in that case.

Please don't swallow exceptions. This will make it very hard to debug in maintenance mode.

catch(RequestExecutionException x)
{
   throw new DBException("Error message",x);
}

See how an exception is converted into another one and retrhown. The following is not right

catch(RequestExecutionException x)
{
   //do nothing and dupe the compiler
}

As you work with resources such as database cursors or connections make sure you close them before leaving your functions. Only way to accomplish this is to do them in the finally clause.

Especially when your function uses database cursors in various for loops, it gets odd to close collections. In that case reorg your code into multiple functions so that each function handles one set of resource usage.

function() {
 try {
    throw SomeTypedException();
 }
 catch (SomeTypedException x) {
 }
}

Prefer

List columnValuesList;

to

List columnValues;

Make sure classes belong to the package that they are in.

Document packages in Java

Search for: Document packages in Java

Use package-info.java to do this


//package-info.java
/**
* Comments
*/
package com.foo;
//Thats all

/**
 * This package contains driver classes to test the 
 * direct SQL access in Android.
 * 
 * These classes use the interface based persistence services
 * from the com.androidbook.provider.services package.
 *
 * These actual services are implemented in the package
 * com.androidbook.provider.services.impl.sqlite
 */
package com.androidbook.provider.directaccess;

So, make sure packages are documented

Make sure classes are documented

However, Keep the documentation brief, and specific

These classes use each other. Ideally every class has a reference to the others and what they do. They collectively does something useful. May be give them their own package. But sometimes that is an overkill.