任何人都可以帮助优化此代码,以减少if_else语句切换案例,因为Sonar显示主要缺陷和NpathComplexity。
if (status != null) {
if (MDOConstants.DOCREATED.equalsIgnoreCase(status)) {
return null;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.MDO_Stored.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.GCSS_SUBMITTED.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.PENDING_TASKS.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.ISSUANCE_OK.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.GCSS_REQUESTED.equalsIgnoreCase(subStatus)) {
return MDOConstants.ISSUANCE_REQUESTED;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.GCSS_ISSUED.equalsIgnoreCase(subStatus)) {
return MDOConstants.ISSUANCE_REQUESTED;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.DEADLINE_PASSED.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.DEADLINE_PASSED_OR_NEW_PICKUPDATE_NEEDED.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.MNLHNDLD.equalsIgnoreCase(status)
&& MDOConstants.MNLHNDLD_TECHNICAL.equalsIgnoreCase(subStatus)) {
return MDOConstants.MNLHNDLD;
} else if (MDOConstants.MNLHNDLD.equalsIgnoreCase(status)
&& MDOConstants.MNLHNDLD_OUTSIDE_MDO.equalsIgnoreCase(subStatus)) {
return MDOConstants.MNLHNDLD;
} else if (MDOConstants.DOISSUED.equalsIgnoreCase(status)) {
return MDOConstants.DOISSUED;
}
}
答案 0 :(得分:4)
首先,由于性能不佳,您不应该重构此代码;或声纳抱怨。
你应该重构它,原因很简单,像这样的东西不可读。人们可以在这样的代码中隐藏3,5个拼写错误,并且数十人会忽视这一点。
那么,你想做什么:应用single layer of abstraction原则。
因此,返工可能如下所示:
public Whatever findWhateverForPotentiallyNull(String status) {
if (status == null) {
return null;
return findWhatEverFor(status.toUpperCase());
}
private Whatever findWhateverFor(String ucStatus) {
if (ucStatus.equals(DOCREATED)) {
return null;
}
if (ucStatus.equals(INPROGRESS)) {
return findWhateverForSubStatus(substatus);
}
......等等。注意:
equals()
比equalsIgnoreCase()
便宜;所以不要一直做晚些时候;只需确保您的传入状态与常量具有相同的效果超越纯粹的重构:
答案 1 :(得分:1)
我想出了类似的东西
if (status != null)
{
if (MDOConstants.DOCREATED.equals(status))
{
return null;
}
else if(MDOConstants.INPROGRESS.equals(status))
{
if(MDOConstants.MDO_Stored.equals(subStatus) ||
MDOConstants.GCSS_SUBMITTED.equals(subStatus) ||
MDOConstants.PENDING_TASKS.equals(subStatus) ||
MDOConstants.ISSUANCE_OK.equals(subStatus) ||
MDOConstants.DEADLINE_PASSED.equals(subStatus) ||
MDOConstants.DEADLINE_PASSED_OR_NEW_PICKUPDATE_NEEDED.equals(subStatus))
{
return MDOConstants.INPROGRESS;
}
else if(MDOConstants.GCSS_REQUESTED.equals(subStatus) ||
MDOConstants.GCSS_ISSUED.equals(subStatus))
{
return MDOConstants.ISSUANCE_REQUESTED;
}
}
else if(MDOConstants.MNLHNDLD.equals(status))
{
if(MDOConstants.MNLHNDLD_TECHNICAL.equals(subStatus) ||
MDOConstants.MNLHNDLD_OUTSIDE_MDO.equals(subStatus))
{
return MDOConstants.MNLHNDLD;
}
}
else if(MDOConstants.DOISSUED.equals(status))
{
return MDOConstants.DOISSUED;
}
}
答案 2 :(得分:1)
如果这是您的确切业务逻辑,与上面的所有返回路径相反,您的代码实际上只返回5个不同的值,一个为null,另外四个为状态。
所有子状态中只有2个甚至在状态结果中也有所不同。
如果上面的代码是正确的,那么这段代码将实现完全相同的结果:
if (status == null || MDOConstants.DOCREATED.equalsIgnoreCase(status))
return null;
if (MDOConstants.INPROGRESS.equalsIgnoreCase(status))
{
if (MDOConstants.GCSS_REQUESTED.equalsIgnoreCase(subStatus) || MDOConstants.GCSS_ISSUED.equalsIgnoreCase(subStatus))
return MDOConstants.ISSUANCE_REQUESTED;
else
return MDOConstants.INPROGRESS
}
else if (MDOConstants.MNLHNDLD.equalsIgnoreCase(status)
return MDOConstants.MNLHNDLD;
else if (MDOConstants.DOISSUED.equalsIgnoreCase(status))
return MDOConstants.DOISSUED;
//Maybe have some default if all else fails here.
答案 3 :(得分:0)
if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.MDO_Stored.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.GCSS_SUBMITTED.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.PENDING_TASKS.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)
&& MDOConstants.ISSUANCE_OK.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
}
这是重要的。你总是先做同样的检查。只需在此块中进行一次,即可进行特定测试。
if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)){
if(MDOConstants.MDO_Stored.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.GCSS_SUBMITTED.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.PENDING_TASKS.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
} else if (MDOConstants.ISSUANCE_OK.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
}
}
然后,你有很多return
,所以你可以存储结果返回,这更容易跟踪(但这是个人的)。
当然,你只返回几个不同的值,你可以使用一个大的条件
if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)){
if(MDOConstants.MDO_Stored.equalsIgnoreCase(subStatus) ||
MDOConstants.GCSS_SUBMITTED.equalsIgnoreCase(subStatus) ||
MDOConstants.PENDING_TASKS.equalsIgnoreCase(subStatus)) ||
MDOConstants.ISSUANCE_OK.equalsIgnoreCase(subStatus)) {
return MDOConstants.INPROGRESS;
}
}
最后,我喜欢用Collection或Arrays编写它。
static final List<String> STATUS_IN_PROGRESS = Arrays.asList(
MDOConstants.MDO_Stored.toLowerCase(),
MDOConstants.GCSS_SUBMITTED.toLowerCase(),
MDOConstants.PENDING_TASKS.toLowerCase(),
MDOConstants.ISSUANCE_OK.toLowerCase()
);
if (MDOConstants.INPROGRESS.equalsIgnoreCase(status)){
if(STATUS_IN_PROGRESS.contains(subStatus.toLowerCase()){
return MDOConstants.INPROGRESS;
}
}
答案 4 :(得分:0)
您可以尝试创建HashMap<Condition,String>
class Condition
{
String status;
String subStatus;
public Condition(String s, String subS)
{
status = s;
subStatus = subS;
}
public boolean equals(Condition c)
{
return c.status.equalsIgnoreCase(status)
&& c.subStatus.equalsIgnoreCase(subStatus);
}
}
map.put(new Condition(MDOConstants.INPROGRESS, MDOConstants.DEADLINE_PASSED), MDOConstants.INPROGRESS);
//...
//...
map.get(new Condition(MDOConstants.INPROGRESS, MDOConstants.DEADLINE_PASSED)); // This will give you result.
当然,还有改进的余地,但你可能会得到一些暗示。