简体   繁体   English

简化 Java IF-ELSE

[英]Simplifying Java IF-ELSE

Imagine there is an "if statement" with n conditions:想象有一个带有 n 个条件的“if 语句”:

if (a == b || x == '0' || z == '1' .... || s == 'e') {      // < n conditions 
    result = do_something(); 
}

Can this be re-written as below :这可以重写如下:

switch (??) {
   case a == b:
   case x == '0':
   case z == '1';
   ...
   case s == 'e':
        result = do_something();
        break;
   default:
        break;
}

It feels more readable, and less cumbersome than multiple conditions separated by OR/AND operators.与由 OR/AND 运算符分隔的多个条件相比,它感觉更易读,也更不麻烦。 If another condition needs to be added then we can just add another case.如果需要添加另一个条件,那么我们可以添加另一个案例。

Is this possible?这可能吗? If yes, then please share an implementation.如果是,那么请分享一个实现。
OR或者
Is the original "if statement" itself a result of bad coding and hence, should be taken as a hint that the entire code needs to be revisited and improved?原始的“if 语句”本身是否是错误编码的结果,因此应该被视为整个代码需要重新访问和改进的提示?

The short answer is no you can not and you also should not .简短的回答是不,你不能,你也不应该 You can simply format it in a nice way:您可以简单地以一种很好的方式对其进行格式化:

if (a == b 
   || x == '0' 
   || (z == '1' &&  a == 'e')
   || s == 'e') {      
    result = do_something(); 
}

The long answer is you could but it would be messy and unclear!长答案是你可以,但它会很混乱和不清楚!

In Java (and most other languages) switch is for 'switching' over an enumeration .在 Java(和大多数其他语言)中,开关用于在枚举上“切换”。 So you can not switch with boolean expressions like x == b .所以你不能用像x == b这样的布尔表达式来切换。 Not even simple things like this work:甚至像这样的简单事情都没有:

 //NOT REAL JAVA CODE
 switch (true) {
        case true:
            Sytem.out.println("true");
            break;
        case false:
            Sytem.out.println("false");
            break;
    }

So the only way left would be to create a repsentative enum... Wich is a bad Idea for all cases i can think of.所以剩下的唯一方法是创建一个代表枚举......对于我能想到的所有情况,这都是一个坏主意。

If an if really escalates consider the following ideas for simplification如果 if 真的升级,请考虑以下简化想法

  • comment more评论更多
  • Use loops instead改用循环
  • Use more Ifs instead使用更多 Ifs 代替
  • Use a truth table to simplify the if使用真值表简化 if

The switch statement is a good candidate for cases when we have a limited number of options in a pre-defined set (eg: days of the week).当我们在预定义集合中的选项数量有限时(例如:一周中的几天),switch 语句是一个很好的候选者。 We can't compare all the types of objects and primitives in the switch statement.我们无法在 switch 语句中比较所有类型的对象和原语。 We can't pass the null value as an argument to a switch statement.我们不能将空值作为参数传递给 switch 语句。 If we do it, the program will throw NullPointerException.如果我们这样做,程序将抛出 NullPointerException。

You will find more such things if you search.如果你搜索,你会发现更多这样的东西。 So, just for sake of readability, don't change if statement to switch case.所以,为了可读性,不要改变 if 语句来切换大小写。 You can make if statement more readable by well formatting, adding comments on if block mentioning the condition use, using nested if.您可以通过良好的格式设置、在 if 块上添加注释以提及条件使用、使用嵌套 if 使 if 语句更具可读性。

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

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