A R
A R

Reputation: 11

Sonarqube Refactor the below code to reduce its cognitive complexity. Complexity is 33, we should reduce to 15

As the below code is having nested if conditions, sonarcube complexity is increasing.

Need to reduce the cognitive complexity from 33 to 15 allowed.

I have tried placing only one try block and one catch block but I didn't work as much as I expected.

public void retryDrools() throws InterruptedException {

    String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();

    List<JeopardyRepositoryRequest> fetchedJeopardies = null;

    JepMessage jepMessage = null;

    JeopardyRepositoryRequest newjepRequest = null;

    String succesgStatus = null;

    String failureMessage = null;

    Date now = new Date();

    String time = new SimpleDateFormat("yyyy-,vldd'T'HH:mm:ss.SSS", Locale.ENGLISH).format(now);

    Loggingutil.Loginfo(className.rmethodName, "Cron Job retryDrools started at " + time);

    try {

        fetchedJeopardies = jeopardyService.getJeopardyByIsDroolsCaliSuccessAndJeopardyStatus(STATUS_OPEN, NO);

    } catch (JeapardyException e) {

        LoggingUtil.LogError(className.rmethodName, "Error Occured while fetching jep by isDroolsCaliSuccessAndJeopardyStatus'");

    }

    for (JeopardyRepositoryRequest retryJepforDroolsCall : fetchedJeopardies) {

        try {

            LiSt<CustomerOrder> customerOrderList = jeopardyService.getCustomerOrdersByBanOrCustonerOrderNumber(retryJepforDroolsCall.getBan(), retryJepforDroolsCall.getCustomerOrderNumber());

            if (!isEmptyOrNuLL(retryJepforDroolsCaIl.getSfdcAccountId()) && !retryJepforDroolsCall.getSfdcAccountId().equalsIgnoreCase("dummy")) {

                string customerType = jeopardyService.checkIfConsumerTypeIsSMBPots(customerOrderList) ? BUSINESS : INDIVIDUAL;

                jepMessage = jeopardyService.checkForSFCCase(retryJepforDroolsCall, customerOrderList, customerType);

                LoggingUtil.Loginfo(className.rmethodName, "Response from checkForSFCCase " + jepMessage);

            }

            if (jepMessage.getAdditionalAttributeList() != null && jepMessage.getAdditionalAttributeList().size() > 0) {

                AdditionalAttributeList droolsCaliSuccessAttribute = jepMessage.getAdditionalAttributeList().stream.filter(retryJepAttribList -> new String("droolscallSuccess").equalsIgnoreCase(retryJepAttribList.getAttributeName())).findFirst().orEise(null);

                AdditionalAttributeList droolsCaliFailureMessageAttribute = jepMessage.getAdditionalAttributeList().stream().filter(retryJepAttribList -> new String("droolsFailure").equalsIgnoreCase(retryJepAttribList.getAttributeName())).findFirst().orEise(null);

                if (droolsCaliFailureMessageAttribute != null) {

                    failureMassage = droolsCaliFailureMessageAttribute.getAttributeValue();

                }

                if (droolsCaliSuccessAttributel: null) {

                    successStatus = droolsCaIISuccessAttribute.getAttributeValue();

                }

                retryJepforDroolsCall.setAdditionalAttributeList(jepMessage.getAdditionalAttributeList());

                LoggingUtil.Loginfo(className.rmethodName, "Setting droolsCaliSuccessAttribute in JeopardyRepositoryReqr" + droolsCaIISuccessAttribute + droolsCaliFailureMessageAttribute);

                if (jepMessage != null) {

                    String sfdcCaseApplicable = (jepMessage.getIsSfdcCaseApplicable() == true) ? YES : NO;

                    retryJepforDroolsCall.setSfdcCaseApplicable(sfdcCaseApplicable);

                }

                if (!isEmptyOrNull(successStatus) && YES.equalsIgnoreCase(successStatus)) && jepMessage != null && jepMessage.getIsSfdcCaseApplicable()) {

                newjepRequest=sfdcCaseCreationAdapter.createOrUpdateCaseinSFDC(retryJepforDroolsCall, jepMessage, "create") ;
                
                if(newjepRequest! =null&& newjepRequest.getSfdCaseId()! =null && newjepRequest.getSfdcCaseNumber! =null) {
                
                retryJepforDroolsCall. setSfdcCaseId(newjepRequest.getSfdCaseId()) ;
                
                retryJepforDroolsCall. setSfdcCaseNumber(newjepRequest.getSfdCaseNumber()) ;
                
                retryJepforDroolsCall. setSfdcCaseCreationStatus(SUCCESS) ;}
                
                else if(newjepRequest! =null&& newjepRequest.getSfdCaseError()! =null) 
                
                {
                
                retryJepforDroolsCall. setSfdcCaseError(newjepRequest.getSfdCaseError()) ;
                
                retryJepforDroolsCall. setSfdcCaseCreationStatus(FAILURE) ;}
                
                }
                
                genericWrapper.updateJeopardyById(retryJepforDroolsCall) ;
                
                LoggingUtil.LogInfo(className.l, methodName, "saving jeopardy");
            
            } catch (Exception e) {
                
                LoggingUtil.LogInfo(className.l, methodName, " Error occurred "+ failureMessage+e.getMessage()) ;
            }
        
        }
    
    }

Upvotes: -2

Views: 3104

Answers (1)

SIMULATAN
SIMULATAN

Reputation: 959

You're supposed to reduce complexity by organizing the method into multiple functions. This ensures that you don't have a huge number of nested IFs and things like that which makes the code harder to read.

Upvotes: 0

Related Questions