【问题标题】:Method refactor need to remove multiple if conditions方法重构需要去掉多个if条件
【发布时间】:2021-11-20 23:30:25
【问题描述】:

我在下面有一个旧方法。需要重构这个方法

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());
            }
        }

我必须重构这个方法。因为声纳提出了“认知复杂性”问题。我试图从“switch”语句中替换所有“if”。但是在每个 if 条件中检查“containskey”。所以我不确定这是否是一个好方法。

任何解决方案都会更有帮助,谢谢。

【问题讨论】:

  • 可能是一个循环迭代elementCollection.keySet()?这是非常难以推理的代码。一个“ZTA”与任何其他“ZTA”有何不同?
  • 这是旧代码。是我使用 elementCollection.keySet(),获取唯一的键列表。我怎样才能匹配条件? 'ZTA' 也是一个固定的字符。

标签: java sonarqube


【解决方案1】:

我怀疑如果您将 Pattern 编译为 grok 输入,您会发现这更容易。例如,如果您匹配“ZTA”并将数字分组怎么办。然后循环使用它。喜欢,

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;
        // ...
        }
    }
}

注意:如果数字实际上是整数值(例如 ZTA0ZTA000 相同),那么

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

【讨论】:

  • 谢谢。 “ZAT”是数字和字符的组合,如ZAT001、ZAT201等。最后三位是唯一的,不重复。再次感谢这个解决方案。
猜你喜欢
  • 2010-11-15
  • 2023-04-04
  • 2022-11-28
  • 1970-01-01
  • 1970-01-01
  • 2014-02-14
  • 2021-08-16
  • 2020-01-30
  • 1970-01-01
相关资源
最近更新 更多