简体   繁体   English

将多个if-else语句转换为更具功能性

[英]convert multi if-else statements to be more functional

How does one make an if else statement functional 如何使if else语句起作用

boolean condition1;
boolean condition2;

final Object a = new Object();
final Object b = new Object();
final Object c = new Object();
final Object d = new Object();

if (condition1 && condition2) {
   return a;
}
else if (condition1 && !condition2) {
   return b;
}
else if (!condition1 && condition2) {
   return c;
}
else {
   return d;
}

I would like to know how can one refactor this type of conditional statements to be more functional, with no overhead in performance preferbly. 我想知道如何能够将这种条件语句重构为更有效的功能,而没有性能上的开销。

I was thinking of Mapping predicates to the object, would this be an approach? 我在考虑将谓词映射到对象,这是一种方法吗?

I've added 2 more conditions to spice it up a bit. 我添加了2个条件,为它添加了一些趣味性。 The if/else statements can get pretty in-depth, so you can use bit-wise to clarify things and merge the conditions. if / else语句可能会非常深入,因此您可以使用逐位语句来澄清事物并合并条件。 To me, this clarifies the code - cause it's one level deep, no matter how many conditions. 对我来说,这使代码更清晰-不管有多少条件,它都是一个层次的代码。

final int BIT_CONDITION_1 = 0x01;
final int BIT_CONDITION_2 = 0x02;
final int BIT_CONDITION_3 = 0x04;
final int BIT_CONDITION_4 = 0x08;


boolean condition1 = false;
boolean condition2 = false;
boolean condition3 = false;
boolean condition4 = false;

int mergedConditions = 0;
if (condition1)
    mergedConditions |= BIT_CONDITION_1;
if (condition2)
    mergedConditions |= BIT_CONDITION_2;
if (condition3)
    mergedConditions |= BIT_CONDITION_3;
if (condition4)
    mergedConditions |= BIT_CONDITION_4;
// continue as needed

// now you can check all conditions using the set bits.
switch(mergedConditions) {
    case 0:      // no bits set
        System.out.println("No bits set");
        break;
    case 1:
        System.out.println("Conditions set = 1");
        break;
    case 2:
        System.out.println("Conditions set = 2");
        break;
    // You can also clarify case statements by using constants
    case (BIT_CONDITION_1 | BIT_CONDITION_2):
        System.out.println("Conditions set = 1,2");
        break;
    case 4:
        System.out.println("Conditions set = 3");
        break;
    case 5:
        System.out.println("Conditions set = 1,3");
        break;
    case 6:
        System.out.println("Conditions set = 2,3");
        break;
    case 7:
        System.out.println("Conditions set = 1,2,3");
        break;
    case 8:
        System.out.println("Conditions set = 4");
        break;
    case 9:
        System.out.println("Conditions set = 1,4");
        break;
    case 10:
        System.out.println("Conditions set = 2,4");
        break;
    case 11:
        System.out.println("Conditions set = 1,2,4");
        break;
    // etc ... Continue as needed
        }

For two conditions there's nothing wrong with your if/then/else statements. 对于两个条件,您的if / then / else语句没有错。 If you have more conditions, there's a way to "simplify" the code using a truth table. 如果您有更多条件,则可以使用真值表“简化”代码。

public Object method()
{
    Object objects[] = { a, b, c, d }; // Assuming objects a, b, c and d exist...

    boolean condition1;
    boolean condition2;

    /* 
     * Truth Table
     * 
     *    condtion1  condition2   Object
     *      false      false        d
     *      false      true         c
     *      true       false        b
     *      true       true         a 
     */

    int selector = (condition1 ? 0 : 1) + (condition2 ? 0 : 2);

    return objects[selector];
}

Not sure if this is what you had in mind but it's a typical way to dispatch multiple conditions. 不确定这是否是您的初衷,但这是分派多个条件的一种典型方法。 While at first glance it looks arcane, if you document the entire truth table for all conditions it can be very safe, since it forces you to consider ALL possible combinations. 乍一看,它看起来很神秘,但是如果您为所有条件记录整个真值表,那将是非常安全的,因为它迫使您考虑所有可能的组合。 This can really help when you have more than two conditions. 当您有两个以上条件时,这确实可以提供帮助。

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

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