简体   繁体   English

简化条件逻辑以提高可读性

[英]Simplifying conditional logic for readibility

I have following code that sets vrstaProizvoda . 我有以下代码设置vrstaProizvoda

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    String vrstaProizvoda = null;
    if (kreditJeAktivanKod != null && kreditJeAktivanKod.equals("Y")) {
        vrstaProizvoda = VrstaProizvoda.STEP.value();
    } else if (idArmPlana != null && !idArmPlana.isEmpty() && !idArmPlana.equals("0000")){
        vrstaProizvoda = VrstaProizvoda.ARM.value();
    }
    return vrstaProizvoda;

}

Looking at else if statement, everything is negating values. 查看else if语句,一切都在否定值。 Is there a better way to write idArmPlana condition so it is easier to read? 有没有更好的方法来编写idArmPlana条件,以便于阅读? Or is it not worth it? 还是不值得?

You could write something along the lines of: 您可以按照以下方式写一些东西:

!(idArmPlana == null || idArmPlana.isEmpty() || idArmPlana.equals("0000"))

The logic is still the same, but it is slightly more readable. 逻辑仍然相同,但可读性更高。 Having long chains of and's or or's is never super readable, but doing something like this where you have your simple conditions, or them together, then negate the result can work. 拥有长的and's or or's链永远不是超级可读的,但是在您拥有简单条件或将它们在一起的情况下进行类似操作,然后取反结果就可以了。

To make it easier to read, just create small functions that have a logical name: 为了更易于阅读,只需创建具有逻辑名称的小函数:

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    String vrstaProizvoda = null;
    if (isYes(kreditJeAktivanKod)) {
        vrstaProizvoda = VrstaProizvoda.STEP.value();
    } else if (!isZero(idArmPlana)){
        vrstaProizvoda = VrstaProizvoda.ARM.value();
    }
    return vrstaProizvoda;

}

function boolean isYes(String string){
  return (null != string && string.equals("Y");
}

function boolean isZero(String string){
 return (null != string && !string.isEmpty() && string.equals("0000");
}

Not entirely sure about Arrays.asList , but the recurring idArmPlana could be used just once: 不能完全确定Arrays.asList ,但是重复使用的idArmPlana只能使用一次:

return "Y".equals(kreditJeAktivanKod)
        ? VrstaProizvoda.STEP.value()
        : !Arrays.<String>asList("", "0000", null).contains(idArmPlana)
        ? VrstaProizvoda.ARM.value()
        : null;

Using Apache commons-lang3 library you can: 使用Apache commons-lang3库,您可以:

import org.apache.commns.lang3.StringUtils;

if (StringUtils.isNotBlank(StringUtils.stripStart(idArmPlana,"0")))

stripStart stolen from How to remove leading zeros from alphanumeric text? 如何从字母数字文本中删除前导零的 stripStart被盗

My preference is one of these: 我的偏好是以下之一:

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
        return VrstaProizvoda.STEP.value();
    } else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
        return VrstaProizvoda.ARM.value();
    } else {
        return null;
    }
}

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    if ( strEquals(kreditJeAktivanKod, "Y") ) {
        return VrstaProizvoda.STEP.value();
    } else if ( !strIsEmpty(idArmPlana) && !strEquals(idArmPlana, "0000") ) {
        return VrstaProizvoda.ARM.value();
    } else {
        return null;
    }
}

Here are a number of rewrites to show a range of alternatives, and to show how to reach the above with incremental adjustments: 这是许多重写,以显示一系列替代方法,并显示如何通过增量调整达到上述要求:

Rewritten with more spaces and parenthesis. 用更多的空格和括号重写。 This makes it easier to pick out the long variable names, and relieves the reader of all need to organize the expression logic: 这使得更容易选择长的变量名,并减轻了读者组织表达式逻辑的所有需要​​:

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    String vrstaProizvoda = null;
    if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
        vrstaProizvoda = VrstaProizvoda.STEP.value();
    } else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
        vrstaProizvoda = VrstaProizvoda.ARM.value();
    }
    return vrstaProizvoda;
}

Rewritten to remove the default 'null' value. 重写以删除默认的“ null”值。 Having such a value is problematic. 具有这样的价值是有问题的。 Consider if the logic were much more complex. 考虑一下逻辑是否更为复杂。 Having a default value takes away the opportunity for the compiler to detect unhandled cases. 具有默认值将使编译器没有机会检测未处理的情况。

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    String vrstaProizvoda;
    if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
        vrstaProizvoda = VrstaProizvoda.STEP.value();
    } else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
        vrstaProizvoda = VrstaProizvoda.ARM.value();
    } else {
        vrstaProizvoda = null;
    }
    return vrstaProizvoda;
}

Rewritten with multiple return values. 用多个返回值重写。 This is my preference, but some prefer a single return statement, as was present in the original method. 这是我的偏爱,但是有些人喜欢使用单个return语句,就像原始方法中那样。

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    if ( (kreditJeAktivanKod != null) && kreditJeAktivanKod.equals("Y") ) {
        return VrstaProizvoda.STEP.value();
    } else if ( (idArmPlana != null) && !idArmPlana.isEmpty() && !idArmPlana.equals("0000") ) {
        return VrstaProizvoda.ARM.value();
    } else {
        return null;
    }
}

Rewritten, with helper methods (see below). 用辅助方法重写(请参见下文)。 This is a little clearer, but at a cost of obscuring the test logic. 这一点更加清晰,但是却以模糊测试逻辑为代价。 Splitting code into a lot of small methods, while often encouraged, is not always preferred in practice. 尽管经常鼓励将代码拆分为许多小的方法,但实际上并不总是首选。

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    if ( strEquals(kreditJeAktivanKod, "Y") ) {
        return VrstaProizvoda.STEP.value();
    } else if ( !strIsEmpty(idArmPlana) && !strEquals(idArmPlana, "0000") ) {
        return VrstaProizvoda.STEP.value();
    } else {
        return null;
    }
}

Helper methods: 辅助方法:

// Test that two strings are equal.  Handle null values.
private boolean strEquals(String value1, String value2) {
    if ( value1 == null ) {
        return ( value2 == null );
    } else if ( value2 == null ) {
        return false;
    } else {
        return value1.equals(value2);
    }
}

// Test that two strings are equal.  Handle null values.
private boolean strEquals(String value1, String value2) {
    boolean result;
    if ( value1 == null ) {
        result = ( value2 == null );
    } else if ( value2 == null ) {
        result = false;
    } else {
        result = value1.equals(value2);
    }
    return result;
}

// Test if a string is neither null nor empty.
private boolean strIsNotEmpty(String value) {
    return ( (value != null) && !value.isEmpty() );
}

To add one more alternative to the already given good answers: 为已经给出的好答案添加另一种替代方法:

private String napraviVrstuProizvoda(String kreditJeAktivanKod, String idArmPlana) {
    return Optional.ofNullable(kreditJeAktivanKod).filter(e->e.equals("Y"))
           .isPresent()? VrstaProizvoda.STEP.value() :
           Optional.ofNullable(idArmPlana).filter(e->!e.equals("0000")).filter(e->!e.isEmpty())
           .isPresent()? VrstaProizvoda.ARM.value(): 
            null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM