Here are the some of the best practices to be followed on order to make your code more managiable and maintainable. These are only the suggessions which will reduce the rework of your code.

Always use comments for your class, methods and variables

One of the best practice is using comments wherever posible. It will make your more understandable not only for you, but also for other developers who use your code.

The another advantage of using the comments is Java Doc. You can generate the Java Doc with a click of your mouse in any IDE which supports it like Eclipse.

Here are the few examples how to use comments in a better way.

Class Comments:

/**
* This class is used to perform operations on database at back-end.
* 1. Updating the database
* 2. Retrieving the data from database
*/
public class DatabaseOperations{
...
}

Method Comments:

/**
* This method is used to updating the database with the data
* given in the form of xml as a parameter.
* @params strXMLQuery the operation to be performed - in the form of xml.
* @returns boolean ...
*/
public boolean DatabaseUpdate(String strXMLQuery){
...
}

Variable Comments:

/** Status of the email with the database*/
private static final boolean DATABASE_STATE_EMAIL;

Always use meaning full names for your classes, methods and variables

Using meaningful names to your methods or variables not only simplyfies your coding tasks but also it makes your code more readable and undestandable.

For example, to declare a method which performs uploading a file from client machine to server, rather than declaring it as upload() declare it as uploadFileToServer() which conveys better meaning and simplifies the task of writing other methods which uses this method.

Do not extend a line to more than acceptable number of characters

The source files should be more readable in order to improve your productivity. One of such activity is writing code in a way such that no line shoud exxtend more than the screen width. i.e you should not drag the horizontal scrollbar in order to see the entire line.

For this you should follow strict line width. Say, Maximum 80 characters per line. If the line exceeds this width, simply devide that line into two parts. That can be done in two ways.

  • Simplify your code so that one line of code can be written in two lines.
  • Wrap the line to second line.

The first way will increase the ease in your code but at the same time you should be very careful. Using this technique excessively will make your code unmanageable.

boolean result = ((test1 != var1 && test2 == var2) || test3 > var3) && test4 == var4)

The above code can be written as below

boolean flag1 = (test1 != var1 & test2 == var2)
boolean flag2= flag1 || (test3 > var3)
boolean result = flag2 && (test4 == var4)

The second technique is far simpler and recomonded. For example, to write

String strResult = (null != strVar1 && null != strVar2 && strVar1.equalsIgnoreCase(strVar2)) ? true : false;

Simply expand it to multiple line as below..

boolean strResult = (null != strVar1 && 
null != strVar2 && 
strVar1.equalsIgnoreCase(strVar2)
) ? true : false;

Use a formatter like jelopy, if posible, to format the java files

Formatting the code using a tool will save lots of time as well as makes the code beautiful so that we can easily understand the code whenever we see it. The tools like Jaloppy are very useful in this aspect. They are not only freeware but also very powerful.


Always use Constants on left side while comparing with variables

Using variables on left side in a comparision will lead to unpredictable results in some cases. For example the code below explains how it is.

intResult = strVar01.equals(CONST_VAR02);
Say for example if the strVar01 is null, then null.equals.. will give the error and your code will not function as you expected.


Tips while using multiple comparisions

Is it always suggested that before performing any opertions on an Object, first check posible errors which can be avoided. Say for example, you are comparing a string value to check whether it is empty or null. It is good to test for null first before empty value. This will avoid the null pointer exceptions.

If you use to test the empty value first, if the value is null then there is a probability to occur the error. If you test null value before that then this case can be avoided.

Example:

boolResult = (null == strVar01 strVar01.equals(""))?true:false;

Never use generic exception to catch exceptions

Always use the speccific exception for the perticular operation so that you can easily debug the error and easily solve if any bug is detected. This will drastically reduce the maintainability. It is a good habit to catch exceptions like null pointer exception wherever possible.

Example:

 

try{
intResult = Integer.parseInt(strVar01);
}catch(NullPointerException objNullPointerException){
//Do what you want to do..
}

 


Like it on Facebook, +1 on Google, Tweet it or share this article on other bookmarking websites.

Comments (1)

  1. Dwarakanathan

Nice article. I always want this kind of article which explain how to start from beginning from no voice. You explained everything with suitable examples.

  Attachments
 
There are no comments posted here yet