【发布时间】: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' 也是一个固定的字符。