Thursday, 1 August 2013

Best Practices to write good Java code

Good java code should be readable and self explanatory to others.
In order to write a good java code,following things are need to be taken care of-

1) Variable Name- should be meaningful(related to context) and should be short.
   avoid hyphen,underscore while declaring variable.
    For ex: int sum,float temperature,double averageSum.

2) If variable name is long,use camel casing. for ex:numberOfRows

3) Class Name- should be noun not verb.It should be descriptive. For ex:ReadMailbox

4) Interface Name -Define Interface names which represents adjective.For ex:Versionable

5) Method Name– Should represent Verb. For ex: getHashItem() , addProject()

6) Comments-Should be written in few words and specific to the topic.

    a)Use implementation comments inside the method  or describing variable names, method line instead of documentation/block comment.
// This code check for valid value of tax if () { }

   b)Use documentation block comment to describe method name or class.

 /** This method retrieves project list  
 public List<String> retrieveProjectList() {  
7) The code should be properly indented and formatted so that it should be readable to programmers.