Wednesday 18 September 2013

Boolean Expression Complexity in SONAR


Boolean Expression Complexity allowed max : 3

Ex:

In below method we need to reduce the complexity to 3:
/*private boolean check(String tempactionUrlStr){
if((tempactionUrlStr != null && tempactionUrlStr.equalsIgnoreCase(Constants.EMPTY_STRING)) ||
(tempactionUrlStr != null && tempactionUrlStr.equalsIgnoreCase(Constants.INDEX_JSP_STR)) ||
(tempactionUrlStr != null && tempactionUrlStr.equalsIgnoreCase(Constants.CALADER_HTML_STR))){
return true;
}
return false;
}*/

Solution:

private boolean check(String tempactionUrlStr){
if(tempactionUrlStr != null){
if(tempactionUrlStr.equalsIgnoreCase(Constants.EMPTY_STRING) ||
tempactionUrlStr.equalsIgnoreCase(Constants.INDEX_JSP_STR)||
tempactionUrlStr.equalsIgnoreCase(Constants.CALADER_HTML_STR)){
return true;
}
else{
return false;
}
}
else{
return false;
}

}


Example 2: 

/*if ((tempactionUrlStr!= null && tempactionUrlStr.endsWith("PNG"))||(tempactionUrlStr!= null && tempactionUrlStr.endsWith("png"))
|| (tempactionUrlStr!= null && tempactionUrlStr.endsWith("GIF")) || (tempactionUrlStr!= null && tempactionUrlStr.endsWith("gif"))
|| (tempactionUrlStr!= null && tempactionUrlStr.endsWith("JPEG")) || (tempactionUrlStr!= null && tempactionUrlStr.endsWith("jpeg"))
|| (tempactionUrlStr!= null && tempactionUrlStr.endsWith("TTF")) || (tempactionUrlStr!= null && tempactionUrlStr.endsWith("ttf"))
|| (tempactionUrlStr!= null && tempactionUrlStr.endsWith("ZIP")) || (tempactionUrlStr!= null && tempactionUrlStr.endsWith("zip"))
|| (tempactionUrlStr!= null && tempactionUrlStr.endsWith("JPG")) || (tempactionUrlStr!= null && tempactionUrlStr.endsWith("jpg"))){
isInvalidRequest = false;
} else {
isInvalidRequest = true;
}*/

Solution : 

if(tempactionUrlStr != null){
if(checkString1(tempactionUrlStr) || checkString2(tempactionUrlStr)){
isInvalidRequest = false;
}
else {
isInvalidRequest = true;
}
}
else {
isInvalidRequest = true;
}
private boolean checkString1(String tempactionUrlStr){
if(tempactionUrlStr.toUpperCase().endsWith("PNG")
|| tempactionUrlStr.toUpperCase().endsWith("GIF")
|| tempactionUrlStr.toUpperCase().endsWith("JPEG")){
return true;
}
return false;
}
private boolean checkString2(String tempactionUrlStr){
if(tempactionUrlStr.toUpperCase().endsWith("TTF")
|| tempactionUrlStr.toUpperCase().endsWith("ZIP")
|| tempactionUrlStr.toUpperCase().endsWith("JPG")){
return true;
}
return false;
}



No comments:

Post a Comment

SpringBoot

SpringBoot SpringBoot Application :  Pros & Cons :  SpringBoot Application creation using spring.io :  SpringBoot Application Annotation...