I have an java POJO class as below.
public class EmployeeVo {
private String firstName;
private String middleName;
private String lastName;
private String suffix;
private String addressline1;
private String addressline2;
private String city;
private String state;
private String zipCode;
private String country;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getMiddleName() {
return middleName;
}
public void setMiddleName(String middleName) {
this.middleName = middleName;
}
public String getLastName() {
return lastName;
}
.// Setters and getters for other fields.
.
.
}
Is there any way to check all the fields of an object at once for null , rather than checking each field for null?
While retrieving these values from EmployeeVo object
public static boolean isEmpty(String string){
if(null==string || string.trim().length()==0){
return true;
}
return false;
}
I have to manually null check each field before using it some where . As the above Object has more than 10 fields it would obviously increase the Cyclomatic Complexity for that method. Is there any optimal solution for it?
if(CommonUtil.isNotEmpty(employeeVO.getCity())){
postalAddress.setCity(employeeVO.getCity());
}
if(CommonUtil.isNotEmpty(employeeVO.getCity())){
postalAddress.setState(employeeVO.getState());
}
if(CommonUtil.isNotEmpty(employeeVO.getZipCode())){
postalAddress.setPostalCode(employeeVO.getZipCode());
}
if(CommonUtil.isNotEmpty(employeeVO.getCountry())){
postalAddress.setCountryCode(employeeVO.getCountry());
}
Can we use
String country=employeeVO.getCountry();
postalAddress.setCountryCode( (country!=null) ? country: "");
Just to reduce the complexity? Is this according to standards?
it is not the responsibility of the caller to decide whether to set a field or not, it should be the method it self.
In your case this means the setters have to decide on the value of the parameter whether to set or not.
This moves the complexity of one method to the single individual methods and therefore decrease the complexity of the caller
This code does not reduce CC. You still have a branch when using ternary.
String country=employeeVO.getCountry();
postalAddress.setCountryCode( (country!=null) ? country: "");
I came up with a simple method for dealing with this.
ifNotBlank(String input, String output);
If the input is blank, then return the output. If the input is NOT blank then return the input. So when applied to your code it would look like this.
postalAddress.setCity(ifNotBlank(employeeVO.getCity(), ""));
postalAddress.setState(ifNotBlank(employeeVO.getState(), ""));
postalAddress.setPostalCode(ifNotBlank(employeeVO.getZipCode(), ""));
postalAddress.setCountryCode(ifNotBlank(employeeVO.getCountry(), ""));
The code for ifNotBlank would look like this.
import org.apache.commons.lang3.StringUtils;
public class ComplexityUtils{
public static String ifNotBlank(String input, String output){
return StringUtils.isBlank(input) ? output : input;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With