I have a method that checks whether an addressCriteria
contains only certain fields or more. If it contains more fields, it should return false, otherwise true.
I'm uncertain what the best way to write my if statement is. As far as I can tell, I see 2 options:
- Write them in 1 if statement
- Write each check in a different if statement
Single if statement:
private boolean isShortAddress(AddressCriteria addressCriteria) {
if((null != addressCriteria.getCity() && !addressCriteria.getCity().isEmpty()) ||
(null != addressCriteria.getNumber() && !addressCriteria.getNumber().isEmpty()) ||
(null != addressCriteria.getState() && !addressCriteria.getState().isEmpty()) ||
(null != addressCriteria.getZipCode() && !addressCriteria.getZipCode().isEmpty())) {
return false;
}
return true;
}
Multiple if statements:
private boolean isShortAddress(AddressCriteria addressCriteria) {
if(null != addressCriteria.getCity() && !addressCriteria.getCity().isEmpty()) {
return false;
}
if(null != addressCriteria.getNumber() && !addressCriteria.getNumber().isEmpty()) {
return false;
}
if(null != addressCriteria.getState() && !addressCriteria.getState().isEmpty()) {
return false;
}
if(null != addressCriteria.getZipCode() && !addressCriteria.getZipCode().isEmpty()) {
return false;
}
return true;
}
Maybe there's a 3rd or 4th option that I am unaware of?
isShortAddress()
poorly named, such that it gives programmers the wrong impression about what it's supposed to do? \$\endgroup\$