Best coding practices for Java developers

Best coding practices for Java developers

In software development it is important that you don’t just write code rather you write a quality code. Quality here means following a set of guidelines or standards to make our code more readable(understandable) and to minimize potential bugs. Here we will see some of the best coding practices for Java developers to know. Following these practices will help you to write a quality code.

Variable and Constant declaration

For variable and constants declarations in the code, follow the below points.

  • Have descriptive variable and constant names used in accord with naming conventions e.g. String studentName;
  • Use constants where a string literal is used in many places.
  • Have appropriate access modifiers (private, protected, public) for the variables.
  • Properly initialize every variable.

Class and method Definition

  • Have a descriptive name for each class and method.
  • Have an appropriate constructor for the classes.
  • Have appropriate access modifiers (private, protected, public) for all the methods.
  • Check every method parameter before being used.
    e.g.
    public void myMethod(String s) {
    If(s!=null){
    // do stuff
    }
  • Have a common set of methods instead of replicating code for these methods.

Computation and comparison

  • Always use parenthesis in the expressions to avoid ambiguity.
  • Always check the order of evaluation and precedence for expressions involving more than one operator.
  • Check if bitwise AND(&) / OR(|) has been inadvertently used instead of logical AND(&&) / logical OR(||).

Control Flow

  • Always have a default case for switch statements. Remember to put break in case statements unless if it is intentionally left.
  • Use for each loop instead of old for loop construct.
  • If there are recursive methods, check for method termination. If there is no condition for termination then a StackOverflowError will be thrown.
  • Use proper exception handling mechanism.
  • Wherever required use finally block with try catch to release resources.
  • If there are multiple if-then-else path then convert it into a switch statement.

Comment – JavaDoc

  • Have an appropriate header comment for every class and method. Mention the purpose of the class and what the method does. The comments should help in understanding the code.
  • Provide appropriate description for any business logic inside the method.
  • Have a comment for every field or constant declaration. Mention what value the variable will hold.

Input/Output operations

  • Always close the files after being used.
  • Check files for existence before attempting to access them.
  • Handle all I/O exceptions in a proper way.

Miscellaneous

  • When comparing string objects with a string literal, always position the literal to the left side of the comparison. This will avoid the null pointer exception.
    e.g. if(“arun”.equals(name)){
  • Check if the cost of re-computing a value be reduced by computing it once and storing the results.
  • When writing if-else statement always write often successful condition inside if statement and less successful conditions inside else.
    e.g. Consider the below case where most of the times the value will be greater than 100.
    if(value >= 100){
    //
    } else if(value >= 50){
    //
    } else {
    //
    }
  • Don’t write a single method with more than 50 lines of code. Move the common set of code into private methods.
  • Do not write more than 1500 lines of code inside a single class.
  • Arrange logical expressions such that the often successful and inexpensive condition precede the more expensive and less frequently successful condition.
    e.g.
    instead of writing if((a || b) && c), write it as if(c && (a || b)) so that if c is false no need to evaluate a || b.
  • Once an object or array is no longer needed, set it to null.
  • When using arrays, always check the code for ArrayIndexOutOfBoundsException.
  • To check whether the list contains an element don’t use code like list.size()>0. Instead write the code as !list.isEmpty(). Of course, you have to check the list for null before doing this. Example,
    if(list!=null && !list.isEmpty()) { /* Do some work */}

 

Make it a habit of writing code following these practices. It will help you to avoid most of the bugs. You can also use static code analyzers to help you to find programming bugs. SonarQube is one such code quality management tool.

Hope you find these coding practices useful. If you have any comments post it in the comments section.

The following two tabs change content below.
Working as a Java developer since 2010. Passionate about programming in Java. I am a part time blogger.

Add Comment

Required fields are marked *. Your email address will not be published.