Friday, May 18, 2012

Filled Under:

5 Most Usefull Java Best Practice Quotes for Java Developers

Quote 1: Avoid creating unnecessary objects and always prefer to do Lazy Initialization

Object creation in Java is one of the most expensive operation in terms of memory utilization and performance impact. It is thus advisable to create or initialize an object only when it is required in the code.


public class Countries {
 
    private List countries;
 
    public List getCountries() {
 
        //initialize only when required
        if(null == countries) {
            countries = new ArrayList();
        }
        return countries;
    }

Quote 2: Never make an instance fields of class public

Making a class field public can cause lot of issues in a program. For instance you may have a class called MyCalender. This class contains an array of String weekdays. You may have assume that this array will always contain 7 names of weekdays. But as this array is public, it may be accessed by anyone. Someone by mistake also may change the value and insert a bug!


public class MyCalender {
 
    public String[] weekdays =
        {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};
 
    //some code
 
}
Best approach as many of you already know is to always make the field private and add a getter method to access the elements.


private String[] weekdays =
    {"Sun", "Mon", "Tue", "Thu", "Fri", "Sat", "Sun"};
 
public String[] getWeekdays() {
    return weekdays;
}
But writing getter method does not exactly solve our problem. The array is still accessible. Best way to make it unmodifiable is to return a clone of array instead of array itself. Thus the getter method will be changed to.


public String[] getWeekdays() {  
  return weekdays.clone();
}
 

Quote 3: Always try to minimize Mutability of a class

Making a class immutable is to make it unmodifiable. The information the class preserve will stay as it is through out the lifetime of the class. Immutable classes are simple, they are easy to manage. They are thread safe. They makes great building blocks for other objects.
However creating immutable objects can hit performance of an app. So always choose wisely if you want your class to be immutable or not. Always try to make a small class with less fields immutable.
To make a class immutable you can define its all constructors private and then create a public static method
to initialize and object and return it.


public class Employee {
 
    private String firstName;
    private String lastName;
 
    //private default constructor
    private Employee(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
 
    public static Employee valueOf (String firstName, String lastName) {
        return new Employee(firstName, lastName);
    }
 
 

Quote 4: Try to prefer Interfaces instead of Abstract classes

First you can not inherit multiple classes in Java but you can definitely implements multiple interfaces. Its very easy to change the implementation of an existing class and add implementation of one more interface rather then changing full hierarchy of class.
Again if you are 100% sure what methods an interface will have, then only start coding that interface. As it is very difficult to add a new method in an existing interface without breaking the code that has already implemented it. On contrary a new method can be easily added in Abstract class without breaking existing functionality.

Quote 5: Always try to limit the scope of Local variable

Local variables are great. But sometimes we may insert some bugs due to copy paste of old code. Minimizing the scope of a local variable makes code more readable, less error prone and also improves the maintainability of the code.
Thus, declare a variable only when needed just before its use.
Always initialize a local variable upon its declaration. If not possible at least make the local instance assigned null value.






 
 

  




0 comments:

Post a Comment