I've been working on a project which has been developed by other developers. In this project, any method that returns an entity or object is designed to return a special value called EMPTY_VALUE
.
public Customer getCustomer() {
if (everythingFine) {
return realCustomer();
} else {
Customer.EMPTY_VALUE;
}
}
And the Customer class:
public class Customer {
public static final Customer EMPTY_VALUE = new Customer();
private String firstName;
private STring lastName;
public Customer() {
this.firstName = "";
this.lastName = "";
}
}
In other places that use the getCustomer() method:
Customer customer = getCustomer();
if (customer != Customer.EMPTY_VALUE) {
doSomething(customer);
}
Does the above way has any advantages over the null
-checking? Does it buy us anything?
Customer customer = getCustomer();
if (customer != null) {
doSomething(customer);
}