Good programming practices in java

In this tutorial you will learn how you can write java code with standard. This tutorial has been written on the personal experience and had taken the advice from experience from industry level.

This tutorial will learn some common basic standards that we should follow while writing java code. Here we start.

Providing Access to Instance and Class Variables

Don’t make any instance or class variable public without good reason. Often, instance variables don’t need to be explicitly set or gotten-often that happens as a side effect of method calls.

One example of appropriate public instance variables is the case where the class is essentially a data structure, with no behaviour. In other words, if you would have used a struct instead of a class (if Java supported struct), then it’s appropriate to make the class’s instance variables public.

Referring to Class Variables and Methods

Avoid using an object to access a class (static) variable or method. Always use a class name instead of making instance and calling method or accessing the variable. For example:


classMethod(); //OK, if the static method is on same class or by static import
AClass.classMethod(); //OK, good
anObject.classMethod(); //AVOID, wrong

Constants

Try to write the Constant class of interface where you can put all constants and then use from constant class or interface.

Better to write a interface for constant, because if you use the interface for constant that interface can be implemented by any class and all the constant directly will available in your class.

Numerical constants (literals) should not be coded directly, except for -1, 0, and 1, which can appear in a for loop as counter values.

Variable Assignments

Avoid assigning several variables to the same value in a single statement. It is hard to read. Example:


fooBar.fChar = barFoo.lchar = 'c'; //avoid to such code

Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example:


if (c++ = d++) { // avoid! (Java will not allow)

}

should be written as


if ((c++ = d++) != 0) {
...
}

Do not use embedded assignments in an attempt to improve run-time performance. This is the job of the compiler. Example:


d = (a = b + c) + r;        // avoid to such code!

should be written as separate statement


a = b + c;
d = a + r;

Miscellaneous Practices

Parentheses

It is generally a good idea to use parentheses liberally in expressions involving mixed operators to avoid operator precedence problems. Even if the operator precedence seems clear to you, it might not be to others-you shouldn’t assume that other programmers know precedence as well as you do.


if (a == b && c == d) // avoid to write such code!
if ((a == b) && (c == d)) // this is right code

Returning Values

Try to make the structure of your program match the intent. Example:


if (booleanExpression) {
return true;
} else {
return false;
}

should instead be written as


return booleanExpression;

Similarly,


if (condition) {
return x;
}
return y;

should be written as


return (condition ? x : y);

Expressions before `?’ in the Conditional Operator

If an expression containing a binary operator appears before the ? in the ternary ?: operator, it should be parenthesized. Example:


(x >= 0) ? x : -x;

Special Comments

Use XXX in a comment to flag something that is bogus but works. Use FIXME to flag something that is bogus and broken.