You need to sign in to do that
Don't have an account?
Mounika@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!
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!
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:
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.
Thanks!
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
Thanks for the reply!
Your code is not working, finally i resolved the issue,
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 + '\',';
}