A hashmap alternative to the “if then else” statement in Java

眉间皱痕 提交于 2020-06-01 07:38:07

问题


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:


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.



来源:https://stackoverflow.com/questions/61578485/a-hashmap-alternative-to-the-if-then-else-statement-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!