1.1 java coding standards and best practices Module Organization

The term module in this context refers to a source file. A source file should contain one public class; it may contain additional non-public classes. So java coding standards is basically refer to java source files. When coding source file is our best practices time(Best practice is to use only one class definition per module.)

The elements of a standards module should be in the following order:

Package name

Import section

Class definition

Class header

Constants (final class variables): public, protected, private

Class variables (private only)

Fields (instance variables) (private only)

Constructors

Other methods.

When ordering java methods, static should be first, public second, protected third.

Java coding standards require you Keep related methods together

When overriding Java superclass functions, keep them in the same order as in the superclass, and preferably together.

Protected inner classes, static or otherwise

Private inner classes, static or otherwise

See also the section on Javadoc comments.

1.2 Standards Module Header

The module header consists of the package name and the import section. With these in place, we can see at a glance what this file is about:

****REVIST, make standard header info.
/**
*
* detailed description of the method
* @param aParam1 – desription of the parameter
* @param aParam2 – desription of the parameter
*
* @return description of the return value
* @exception anException1 – description of the exception
* @exception anException2 – description of the exception
*
* @see some other method or class
* @see some other method or class
*/

In the import section, list each imported module explicitly. The coding standards listing each imported is more readable.

Best practices Example:

Standard Non-Standard
import javax.servlet.RequestDispatcher;import javax.servlet.http.HttpSession;import com.orionauto.autolink.businessobjects.Driver; import javax.servlet.*;import javax.servlet.http.*;import com.orionauto.autolink.businessobjects.*;

Neither of these conventions is consistently maintainable; so don’t put a lot of work into verifying that all listed modules are used. Likewise, don’t spend time converting existing modules from one format to the other.

1.3 Code Layout

A good layout strategy should accurately and consistently represent the logical structure of the code, it should make the code readable, and it should be easy to maintain. This is standards java coding give you advantage. The rules in this section are designed to meet those criteria and best practices.

1.3.1 Class Headers

Write class headers on a single line if there is room for it.

If not, break the line before extends and implements. Indent succeeding lines.

If the class header is on a single line, put the opening brace at the end of that line.

If the class header needs multiple lines, put the opening brace left aligned on a line by itself.

1.3.2 Method Headers

Write method headers on a single line if there is room for it.

If not, break the line immediately after the opening parenthesis. This leaves all the parameters on the same line.

If there still isn’t enough room, put each parameter on its own line.

If the method header is on a single line, put the opening brace at the end of that line.

If the method header needs multiple lines, put the opening brace left aligned on a line by itself.

1.3.3 Indentation

Standard coding indentation should be 4 spaces. Actually, indentation is one tab, which should be set to display as four spaces.

Use tabs for indentation only. Any white space after the indentation level should be actual spaces, so that the formatting will be reasonable no matter how many spaces a tab equals.

1.3.4 White Space in the Code

White spaces in general enhance readability.

Add one space in the following places:

Between operators

After comma in method declarations and invocations

After semicolons in for-loops

Before and after the assignment operator

No space in the following places:

o Between a method name and the opening parenthesis

o Around opening and closing parentheses in a function declaration or invocation

o Around opening and closing square brackets in an array declaration or reference

Best practices Example:

if (!comboValid)
{
this.accounts.removeAll();
for (int i = 0; i < accountList.size(); ++ i)

{

this.accounts.addItem(accountList.get(i).toString())

}

final String accountID = ContextManager.query(SOME_ID);

int index = this.getGroupIDIndex(accountID);

this.accounts.select(Math.max(0, index));

this.comboValid = true;

}

private String titles[] = null; // array of strings

client.height =

size.height – this.insets.top – this.insets.bottom –

this.title.height;

public String getItem(int aRow, int aColumn) {

return (String) this.list[aColumn].elementAt(aRow);

}

Pages: 1 2