一个在Java中替代“if then else”语句的哈希映射方案

huangapple 未分类评论45阅读模式
英文:

A hashmap alternative to the "if then else" statement in Java

问题

有没有人能帮我寻找一种替代 if-then-else 语句用于控制流的方法?或者推荐一篇好的文章?

根据我所看到的,有些人使用映射(mapping)或枚举(enums)。我遇到的问题是,我有多个条件,例如 if (condition1 && condition2 && condition3)...,我需要对多种组合进行这样的操作,而且所有 3 个变量都需要验证。

请有人能指点我正确的方向吗?

else if (String.Utils.isNotEmpty(payload.getKeyChange2TokenNumber()) && String.Utils.isEmpty(payload.getKeyChange1TokenNumber()) && String.Utils.isEmpty(payload.getKeyChange3TokenNumber()){
     根据验证结果返回相应的值
}

因此,如果不使用 if-then-else 语句,我该如何实现哈希映射(hashmap),以确定在代替 if-then-else 语句的返回位置应返回什么内容呢?

谢谢。

英文:

Can anyone assist me with an alternative to if then else statements for control flow? Or advise on a good article?

From what I've seen, some use mapping or enums. Trouble I'm having is that I have multiple conditions i.e. if (condition1 && condition2 && condition3)... and I need to do this for several permutations and all 3 variables need to be validated.

Please can someone point me in the right direction?

else if (String.Utils.isNotEmpty(payload.getKeyChange2TokenNumber()) && String.Utils.isEmpty(payload.getKeyChange1TokenNumber()) && String.Utils.isEmpty(payload.getKeyChange3TokenNumber()){
 String.format(return value dependant on outcome of validation)
}

So no if then else statements, how can I implement a hashmap to determine what to return in place of the if then else statements return

Thank you.

答案1

得分: 0

因此,很多人最终尝试避免使用if语句,因为他们觉得“一定有一种更简单、更干净的方法来做这个”。然而从根本上来说,代码只是在基本层次上一堆if语句。

因此,我不会太担心使用它们,因为如果尝试使用HashMap或其他东西,可能只是在用核弹来杀蚊子。

要记住的一件事是,不要使用嵌套的if/else语句,这会变得难以检查。

针对你的示例,你提到你必须多次对变量进行此检查。那么,在流程开始时检查它们是否为空有什么问题呢?如果它们为空,那么退出或返回相应的结果。然后你就不需要再进行检查了。

此外,使用描述你要做的事情的简短函数是很有用的。

而不是:

else if (String.Utils.isNotEmpty(payload.getKeyChange2TokenNumber())
    && String.Utils.isEmpty(payload.getKeyChange1TokenNumber())
    && String.Utils.isEmpty(payload.getKeyChange3TokenNumber()) {
    String.format(return value dependant on outcome of validation)
}

尝试:

if (notEmpty(payload.getKeyChange2TokenNumber())
    && notEmpty(payload.getKeyChange1TokenNumber())
    && notEmpty(payload.getKeyChange3TokenNumber())) {
    String.format(return value dependant on outcome of validation)
}

private boolean notEmpty(String string) {
    return StringUtils.isNotEmpty(string);
}

另外,如果上述检查实际上与与领域相关的响应有关,则使用该响应。例如,假设getKeyChange1TokenNumbergetKeyChange2TokenNumbergetKeyChange3TokenNumber都经过检查,以确定是否提供了必需的密钥更改令牌号码,如果不是真的,您的代码将如下所示:

public void main() {
    if (mandatoryKeyChangeTokensNotProvided(payload)) {
        return "Mandatory Key Change Tokens not provided";
    }
    ...
}

private boolean mandatoryKeyChangeTokensNotProvided(Payload payload) {
    return isEmpty(payload.getKeyChange2TokenNumber())
            && isEmpty(payload.getKeyChange1TokenNumber())
            && isEmpty(payload.getKeyChange3TokenNumber());
}

private boolean isEmpty(String string) {
    return StringUtils.isEmpty(string);
}

尽量在代码中使用领域语言,这样对开发人员来说更有意义。因此,阅读这段代码的开发人员会阅读mandatoryKeyChangeTokensProvided方法并知道它的作用。如果他们想知道它是如何实现的,只需进入该方法,看到它在其中进行了对字符串是否为空的检查。希望这能帮助你澄清问题。

有多种方法可以做到这一点,但这都取决于你的领域。例如,你说这是验证?使用一个Validator类来执行所有这些检查有什么问题吗?但要记住KISS原则(保持简单),尽量不要过于复杂化。

英文:

So, a lot of people end up trying to avoid using if statements because they feel "there must be an easier and more cleaner way of doing this". However think fundamentally, code is just a bunch of if statements at the basic level.

So I wouldn't be too worried about using them, because by trying to use HashMaps or whatever, then you may be just using a nuclear bomb to kill a mosquito.

One thing to keep in mind is that you don't want nested if/else statements, it does become hard to check.

For your example, you mention that you have to do this check on the variables multiple times. So what's wrong with checking that they aren't empty at the start of the flow. If they are then exit or return with the corresponding result. You then don't need to do the checks again.

Additionally, it is useful to use short functions that describe what you're trying to do.

Instead of:

else if (String.Utils.isNotEmpty(payload.getKeyChange2TokenNumber())
    && String.Utils.isEmpty(payload.getKeyChange1TokenNumber())
    && String.Utils.isEmpty(payload.getKeyChange3TokenNumber()) {
    String.format(return value dependant on outcome of validation)
    }

Try:

 if (notEmpty(payload.getKeyChange2TokenNumber())
        && notEmpty(payload.getKeyChange1TokenNumber())
        && notEmpty(payload.getKeyChange3TokenNumber())) {
    String.format(return value dependant on outcome of validation)
}


private boolean notEmpty(String string) {
    return StringUtils.isNotEmpty(string);
}

Additionally, if the above check is actually related to a domain related response then use that instead. For example, let's say getKeyChange1TokenNumber, getKeyChange2TokenNumber, getKeyChange3TokenNumber are all checked to determine whether the mandatory key change token numbers are provided and you cannot proceed if it isn't true. You're code would look like this:

public void main() {
    if (mandatoryKeyChangeTokensNotProvided(payload)) {
        return "Mandatory Key Change Tokens not provided";
    }
    ...
}

private boolean mandatoryKeyChangeTokensNotProvided(Payload payload) {
    return isEmpty(payload.getKeyChange2TokenNumber())
            && isEmpty(payload.getKeyChange1TokenNumber())
            && isEmpty(payload.getKeyChange3TokenNumber());
        }
private boolean isEmpty(String string) {
    return StringUtils.isEmpty(string);
}

Try to use the domain language in your code so it makes more sense to the dev. So a dev reading this would read the mandatoryKeyChangeTokensProvided method and know what it does. If they want to know how it does it, then just go into the method and see that its doing string empty checks against it. Hopefully this clears things up for you.

There are multiple ways you can do this, but it all depends on your domain. For example, you say this is validation? Whats wrong with having a Validator Class that does all these checks for you? But remember the KISS principle. Try not to overcomplicate things.

huangapple
  • 本文由 发表于 2020年5月4日 01:06:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/61578485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定