Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coding standard with multiple if condition

Tags:

java

I need to set data in entity based on on some condition.

I have used below to set data

if (StringUtils.isNotBlank(customerVO.getGender())) {
    mstCustomer.setGender(customerVO.getGender());
}
if (StringUtils.isNotBlank(customerVO.getBirthDate())) {
    mstCustomer.setDob(DateUtils.getUtilDate(customerVO.getBirthDate()));
}

if (StringUtils.isNotBlank(customerVO.getAdd1())) {
    mstCustomer.setAddress1(customerVO.getAdd1());
}
if (StringUtils.isNotBlank(customerVO.getAdd2())) {
    mstCustomer.setAddress2(customerVO.getAdd2());
}
if (StringUtils.isNotBlank(customerVO.getAdd3())) {
    mstCustomer.setAddress3(customerVO.getAdd3());
}
if (StringUtils.isNotBlank(customerVO.getPincode())) {
    mstCustomer.setPinCode(customerVO.getPincode());
}
if (StringUtils.isNotBlank(customerVO.getStateName())) {
    MstState state = mstStateRepository.findByName(customerVO.getStateName());
    mstCustomer.setMstState(state);
}

if (StringUtils.isNotBlank(customerVO.getCity())) {
    MstCity city = mstCityRepository.findByName(customerVO.getCity());
    mstCustomer.setMstCity(city);
}

if (StringUtils.isNotBlank(customerVO.getIdentificationType())) {
    mstCustomer.setIdentificationType(customerVO.getIdentificationType());
}

if (StringUtils.isNotBlank(customerVO.getIdentificationData())) {
    mstCustomer.setIdentificationData(customerVO.getIdentificationData());
}

MstStatus mstStatus = mstStatusRepository.findOne(MstStatusEnum.CUST_ACTIVE.getStatusCode());
if (mstStatus != null) {
    mstCustomer.setMstStatus(mstStatus);
}

if (!StringUtils.isBlank(customerVO.getMaritalStatus())) {
    mstCustomer.setMaritalStatus(customerVO.getMaritalStatus());
}
if (StringUtils.isBlank(customerVO.getWeddingAnniversary())) {
    mstCustomer.setWeddingAnniversary(DateUtils.getDateFromString(customerVO.getWeddingAnniversary()));
}

if (StringUtils.isNotBlank(customerVO.getMotherTongue())) {
    mstCustomer.setMotherTongue(customerVO.getMotherTongue());
}
if (StringUtils.isNotBlank(customerVO.getFamilySize())) {
    mstCustomer.setFamilySize(Integer.valueOf(customerVO.getFamilySize()));
}
if (StringUtils.isNotBlank(customerVO.getAdultsSize())) {
    mstCustomer.setAdultsSize(Integer.valueOf(customerVO.getAdultsSize()));
}
if (StringUtils.isNotBlank(customerVO.getNoOfKids())) {
    mstCustomer.setNoOfKids(Integer.valueOf(customerVO.getNoOfKids()));
}
if (StringUtils.isNotBlank(customerVO.getChilddob1())) {
    mstCustomer.setChilddob1(DateUtils.getDateFromString(customerVO.getChilddob1()));
}
if (StringUtils.isNotBlank(customerVO.getChilddob2())) {
    mstCustomer.setChilddob2(DateUtils.getDateFromString(customerVO.getChilddob2()));
}
if (StringUtils.isNotBlank(customerVO.getProfession())) {
    mstCustomer.setProfession(customerVO.getProfession());
}

But sonar throwing this exception : Refactor this method to reduce its Cognitive Complexity from 27 to the 15 allowed.

Please suggest what is the best way to refactor above code.

like image 777
Shiladittya Chakraborty Avatar asked Dec 24 '17 14:12

Shiladittya Chakraborty


People also ask

How do you handle multiple if conditions?

Use two if statements if both if statement conditions could be true at the same time. In this example, both conditions can be true. You can pass and do great at the same time. Use an if/else statement if the two conditions are mutually exclusive meaning if one condition is true the other condition must be false.

Can we use multiple if in a code?

You can nest multiple IF() statements to have complex logic chains. But if you need to use more than 7 nested IF() statements, then you can use IFS() instead.

Can you include multiple conditions in an if statement?

The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.

Can IF statement have 2 conditions in C?

There can also be multiple conditions like in C if x occurs then execute p, else if condition y occurs execute q, else execute r. This condition of C else-if is one of the many ways of importing multiple conditions.


1 Answers

Seems pretty doable using lambdas:

private void setIfNotBlank(String value, Consumer<String> setter)  {
    setConditionally(value, setter, StringUtils::isNotBlank);
}

// if you don't need non-string arguments you can inline this method
private <T> void setConditionally(T value, Consumer<T> setter, Predicate<T> shouldSet) {
    if (shouldSet.test(value)) setter.accept(value);
}

Then,

if (StringUtils.isNotBlank(customerVO.getBirthDate())) {
    mstCustomer.setDob(DateUtils.getUtilDate(customerVO.getBirthDate()));
}

if (StringUtils.isNotBlank(customerVO.getCity())) { 
    MstCity city = mstCityRepository.findByName(customerVO.getCity()); 
    mstCustomer.setMstCity(city); 
}

would become

setIfNotBlank(customerVO.getBirthDate(), birthDate -> mstCustomer.setDob(DateUtils.getUtilDate(birthDate)));
setIfNotBlank(customerVO.getCity(), cityName -> { 
    MstCity city = mstCityRepository.findByName(cityName); 
    mstCustomer.setMstCity(city); 
});
like image 84
Dici Avatar answered Oct 24 '22 00:10

Dici