Search code examples
javasonarqube

Method refactor need to remove multiple if conditions


I have a old method below. Need to refactor this method

private Scheme <Method Name>(Input input, <classname extends HashTable> elementCollection, Map<String, Object> loadValue, String imt) {
        Scheme scheme = null;

        if (input.isInfo()) {
            if (elementCollection.containsKey("ZTA216")) {
                <Some Logic>
            } else if (imt.equals("4124")) {
               <Some Logic>
            }
        }
        if (elementCollection.containsKey("ZTA001")) {
            try {
               <Some Logic>
            } catch (Exception e) {
                scheme = null;
            }
        }
        if (elementCollection.containsKey("ZTA000")) {
           <Some Logic>
        }

        if (elementCollection.containsKey("ZTA201")) {
            <Some Logic>
        }
        if (elementCollection.containsKey("ZTA211")) {
           <Some Logic>
        }
        if (elementCollection.containsKey("ZTA210")) {
           <Some Logic>
        }
        String acquirer_id = null;
        if (elementCollection.containsKey("ZTA032")) {
            <Some Logic>
        }
        if (scheme != null) {
            <Some Logic>
        }
        if (elementCollection.containsKey("ZTA204")) {
            <Some Logic>
        }
        

        if (elementCollection.containsKey("ZTA217")) {
            <Some Logic>
        }
        if (elementCollection.containsKey("ZTA218") && !(imt.equals("1120") || imt.equals("1420") || imt.equals("1220"))) {
           <Some Logic>
        }
        if (elementCollection.containsKey("ZTA219")) {
           <Some Logic>
        }
        if (elementCollection.containsKey("ZTA220")) {
            <Some Logic>
        }
        if (elementCollection.containsKey("ZTA221")) {
            <Some Logic>
        }
        if (elementCollection.containsKey("ZTA222")) {
           <Some Logic>
        }
        if (elementCollection.containsKey("ZTA224")) {
          <Some Logic>
        }
        if (elementCollection.containsKey("ZTA225")) {
           <Some Logic>
        }
        if (elementCollection.containsKey("ZTA223")) {
            try {
               <Some Logic>
            } catch (NumberFormatException e) {
                log.warn("Error Mesage", elementCollection.get("ZTA223").getStringValue());
            }
        }

I have to refactor this method. Because sonar raising issue "Cognitive Complexity". I tried to replace all "if" from 'switch' statement. But in every if condition checking 'containskey'. So I am not sure it would be a good approach.

any solution it would be more help thanks.


Solution

  • I suspect you would find this easier if you compiled a Pattern to grok the input for you. For example, what if you matched "ZTA" and grouped the digits. Then used that in a loop. Like,

    Pattern p = Pattern.compile("ZTA(\\d+)");
    for (String key : elementCollection.keySet()) {
        Matcher m = p.matcher(key);
        if (m.matches()) {
            switch (m.group(1)) {
            case "000":
                // Some logic
                break;
            case "001":
                // Some logic
                break;
            case "032":
                // Some logic
                break;
            // ...
            }
        }
    }
    

    Note: If the numbers are actually integer values (e.g. ZTA0 and ZTA000 are the same) then

    switch (Integer.parseInt(m.group(1))) {
    case 0:
        // ...
        break;
    }