Tuesday, July 20, 2010

Instanceof operator kills Object Oriented Programming


Target audience: Beginners

Everybody knows about instanceof operator of Java. I just give a simple description about instanceof operator before get into details. The instanceof operator allows a user to check whether an object is an instance of a particular type.The type could be a class or an interface.

if (obj instanceof type) {
type new_name = (type) obj;
}

Here the obj is an instance of the type then, the condition will be true , else it will be false. This condition will also become false if obj is a null. So better to be careful while using it.

I personally feel that sometimes the instanceof operator is missused and it kind of kills the object oriented concept. Let me explain by taking a simple example similar to what we deal with everyday

Suppose we have abstact class Person and two other sub classes Student and Teacher

abstract class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

class Student extends Person {
void study() {
}
}

class Teacher extends Person {
void teach() {
}
}

Appart from the inherited methods, Student has a method study() and Teacher has a method teach(). Suppose if there is a situation that we have a list of mixed objects of Student and Teacher and we need to call their own methods. We might write a code similar to the following one.

List list = new ArrayList();
list.add(new Teacher());
list.add(new Student());

for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
if (object instanceof Student) {
Student obj = (Student) object;
obj.study();
} else if (object instanceof Teacher) {
Teacher obj = (Teacher) object;
obj.teach();
}
}

This is the way we use the instanceof operator. We can get rid of this if we apply some Object oriented concept.What I'm going to do is, I’m going to change the Person,Student and Teacher classes a bit

abstract class Person {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

abstract void doYourWork();
}

class Student extends Person {

void study() {
}

void doYourWork() {
study();
}
}

class Teacher extends Person {
void teach() {
}

void doYourWork() {
teach();
}
}

I added a abstract method doYourWork() in Person class and let the sub classes Student and Teacher to implement it and call their own methods inside their implementation of doYourWork() method. Now i’m going to change the iterating part also.

List list = new ArrayList();
list.add(new Teacher());
list.add(new Student());

for (Iterator iterator = list.iterator(); iterator.hasNext();) {
Person object = (Person) iterator.next();
object.doYourWork();
}

Wow... the instance of operator is gone and code looks pretty too. I just applied a simple OO concept and got rid of the instanceof operator. This is why i mentioned “I personally feel that sometimes the instanceof operator is missused and it kind of kills the object oriented concept.”

Monday, July 19, 2010

Return Empty Collection than null if there is nothing to return


Target audience: Beginners

Every day we come across at least a method that would return a list of objects or null.For example see the following code block.

Employee employee = ....
List children = employee.getChildren();
if (children != null) {
for (Child child : children) {
// do something....
}
}

Here, the getChildren() of Employee is prone to error because it would return null object. The caller always need to check null before proceeding the returned list. We can try to reduce these situation by returning an empty list if there are nothing to return when a list is expected to be returned. Suppose if the getChildren() return empty list if there is nothing to return, then the caller’s code will be as bellow

Employee employee = ....
List children = employee.getChildren();
for (Child child : children) {
// do something....
}

This looks neat and reduces the possibilities to make errors.

Some people might say that creating an Empty list and returning every time if there is nothing to return, will be costly and unnecessarily creating objects. True, and we are not going to create empty object every time. Collections API has static final empty list which can be used to return instead. Look at code shown bellow to know how it can be done

class Employee {
public List getChildren() {
...
if(children == null){
return Collections.EMPTY_LIST;
}
....
}
...
}
This style can be used for any other collections and of course arrays too. It's not restricted only to List