function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mounika@9999Mounika@9999 

AvoidDeeplyNestedIfStmts: Deeply nested if..else statements are hard to read

Hi All,
Need to resolve the warning, which is 'AvoidDeeplyNestedIfStmts'."

Using the following code, I am getting a warning about
Deeply nested if..else statements are hard to read (rule: Design-AvoidDeeplyNestedIfStmts)apex pmdAvoidDeeplyNestedIfStmts
Example code:
 if(String.isNotBlank(brand)){
                   if(brand.contains(';')){
                       arrTemp = brand.split(';');
                       filterTemp = '';
                       for(integer i=0; i< arrTemp.size(); i++){
                           String valueTemp = arrTemp[i];

                           if(String.isNotBlank(valueTemp)){
                               filterTemp += ' XC_Brand__c = \'' + valueTemp + '\' OR';
                           }                            
                       }
                       if(String.isNotBlank(filterTemp)){
                           filterTemp = filterTemp.removeEnd(' OR');
                           prodFilterString += ',(' + filterTemp + '),';
                       }
                   }else{
                       prodFilterString += ',XC_Brand__c = \'' + brand + '\',';
                   }
                   
               }

"I have highlighted the issue."

Please help me out.
Thanks!
Bryan Leaman 6Bryan Leaman 6
Hmm. That doesn't look all that deeply nested to me. But I believe you can entirely skip the condition of "if (brand.contains(';'))".
if(String.isNotBlank(brand)) {
    //if(brand.contains(';')) {
         arrTemp = brand.split(';');
         prodFilterString += ',( XC_Brand__c = \'' + String.join(arrTemp, '\' OR XC_Brand__c = \'') + '\'),';
         ///filterTemp = '';
         //for(integer i=0; i< arrTemp.size(); i++) {
         //     String valueTemp = arrTemp[i];
         //     if(String.isNotBlank(valueTemp)) {
         //           filterTemp += ' XC_Brand__c = \'' + valueTemp + '\' OR';
         //     }                            
         //}
         //if(String.isNotBlank(filterTemp)) {
         //    filterTemp = filterTemp.removeEnd(' OR');
         //    prodFilterString += ',(' + filterTemp + '),';
         //}
    //} else {
    //    prodFilterString += ',XC_Brand__c = \'' + brand + '\',';
    //}
}
I believe the only difference in the result is that my version places the XC_Brand__c comparisons in parenthesis even if there's only 1.

Based on the syntax, with a comma prior to the open paren, I'm assuming this isn't a SOQL query. If it were, you should simply do something like this: 
List<String> brandList;
if(String.isNotBlank(brand)) {
     brandList = brand.split(';');
     prodFilterString += ' AND  XC_Brand__c in :brandList ';
}

 
Mounika@9999Mounika@9999
Hi Bryan,
Thanks for the reply!

In summary, the modified code does not accurately replicate the functionality of the original code snippet what i have shared. It does not handle multiple brand values or construct filter conditions in the same way as the original code.

I am sharing the original code problem along with a screenshot. Please take a look and help me find out the issue.
AvoidDeeplyNestedIfStmts
 
Thanks!
 
 
Arun Kumar 1141Arun Kumar 1141

Hi Mounika@9999,
you can try the below code:

 

if (String.isNotBlank(brand)) {
    if (brand.contains(';')) {
        String[] arrTemp = brand.split(';');
        List<String> filters = new List<String>();
        
        for (String valueTemp : arrTemp) {
            if (String.isNotBlank(valueTemp)) {
                filters.add('XC_Brand__c = \'' + valueTemp + '\'');
            }
        }
        
        if (!filters.isEmpty()) {
            prodFilterString += ',' + String.join(filters, ' OR ') + ',';
        }
    } else {
        prodFilterString += ',XC_Brand__c = \'' + brand + '\',';
    }
}

Mark this as best answer if this helps.
Thanks

Mounika@9999Mounika@9999
Hi Arun,
Thanks for the reply!

Your code is not working, finally i resolved the issue,
 
if(String.isNotBlank(brand) && brand.contains(';')){
    arrTemp = brand.split(';');
    filterTemp = '';
    for(integer i=0; i< arrTemp.size(); i++){
        String valueTemp = arrTemp[i];
        filterTemp += String.isNotBlank(valueTemp) ? ' XC_Brand__c = \'' + valueTemp + '\' OR' : '';                        
    }
    filterTemp = String.isNotBlank(filterTemp) ? filterTemp.removeEnd(' OR') : '';
    prodFilterString +=  String.isNotBlank(filterTemp) ? ',(' + filterTemp + '),' : '';
}
else if(String.isNotBlank(brand)){
    prodFilterString += ',XC_Brand__c = \'' + brand + '\',';
}