散列图替代"if then else"的散列图. Java语句 [英] A hashmap alternative to the "if then else" statement in Java

查看:94
本文介绍了散列图替代"if then else"的散列图. Java语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以协助我,以其他方式代替控制流程的陈述吗?或就一篇好文章提出建议?

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

根据我所看到的,有些使用映射或枚举.我遇到的麻烦是我有多个条件,即if(condition1&& condition2&& amp; 3)...,并且我需要对几个排列进行此操作,并且需要验证所有3个变量.

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

因此,如果没有if else语句,那么我该如何实现哈希表来确定要返回的内容,以代替if then else语句返回

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

谢谢.

推荐答案

因此,许多人最终都尝试避免使用if语句,因为他们认为必须有一种更简单,更清洁的方法".但是从根本上考虑,代码只是基本级别上的一堆if语句.

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.

所以我不会太担心使用它们,因为通过尝试使用HashMaps或其他方法,您可能只是在使用核弹杀死蚊子.

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.

要记住的一件事是,您不想嵌套if/else语句,这确实变得很难检查.

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.

代替:

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都进行了检查,以确定是否提供了必需的密钥更改令牌号,如果不正确,则无法继续.您的代码如下所示:

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

尝试在代码中使用域语言,以便对开发人员更有意义.因此,阅读此内容的开发人员将阅读mandatoryKeyChangeTokensProvided方法并知道它的作用.如果他们想知道它是如何做到的,那么只需进入该方法,看看它的字符串是否为空就可以对其进行检查.希望这可以为您清除一切.

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.

您可以通过多种方式来执行此操作,但这完全取决于您的域.例如,您说这是验证?拥有可为您进行所有这些检查的Validator类怎么办?但是请记住KISS原则.尽量不要使事情过于复杂.

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.

这篇关于散列图替代"if then else"的散列图. Java语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆