java - 重构复杂的 if 条件

标签 java refactoring conditional if-statement

任何人都可以建议避免大多数 if 条件的最佳方法吗?我有以下代码,如果有条件,我想避免大多数情况,该怎么做?任何解决方案都有很大的帮助;

if (adjustment.adjustmentAccount.isIncrease) {
    if (adjustment.increaseVATLine) {
        if (adjustment.vatItem.isSalesType) {
            entry2.setDebit(adjustment.total);
            entry2.setCredit(0d);
        } else {
            entry2.setCredit(adjustment.total);
            entry2.setDebit(0d);
        }
    } else {
        if (adjustment.vatItem.isSalesType) {
            entry2.setCredit(adjustment.total);
            entry2.setDebit(0d);
        } else {
            entry2.setDebit(adjustment.total);
            entry2.setCredit(0d);
        }
    }
} else {
    if (adjustment.increaseVATLine) {
        if (adjustment.vatItem.isSalesType) {
            entry2.setCredit(adjustment.total);
            entry2.setDebit(0d);
        } else {
            entry2.setDebit(adjustment.total);
            entry2.setCredit(0d);
        }
    } else {
        if (adjustment.vatItem.isSalesType) {
            entry2.setDebit(adjustment.total);
            entry2.setCredit(0d);
        } else {
            entry2.setCredit(adjustment.total);
            entry2.setDebit(0d);
        }
    }
}

最佳答案

如何处理...让我们提取几个方法,这样我们就可以更好地理解逻辑。

private void a() {
    entry2.setDebit(adjustment.total);
    entry2.setCredit(0d);
}
private void b() {
    entry2.setCredit(adjustment.total);
    entry2.setDebit(0d);
}

if (adjustment.adjustmentAccount.isIncrease) {
    if (adjustment.increaseVATLine) {
        if (adjustment.vatItem.isSalesType) {
            a();
        } else {
            b();
        }
    } else {
        if (adjustment.vatItem.isSalesType) {
            b();
        } else {
            a();
        }
    }
} else {
    if (adjustment.increaseVATLine) {
        if (adjustment.vatItem.isSalesType) {
            b();
        } else {
            a();
    }
} else {
    if (adjustment.vatItem.isSalesType) {
        a();
    } else {
        b();
    }
}

所以现在,看看它,第一个 block

if (adjustment.increaseVATLine) {
    if (adjustment.vatItem.isSalesType) {
        a();
    } else {
        b();
    }
} else {
    if (adjustment.vatItem.isSalesType) {
        b();
    } else {
        a();
    }
}

如果 adjustment.increaseVATLineadjustment.vatItem.isSalesType 具有相同的值,则 a()b () 否则。所以我们可以减少它:

if (adjustment.adjustmentAccount.isIncrease) {
    if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
        a();
    } else {
        b();
    }
} else {
    if (adjustment.increaseVATLine) {
        if (adjustment.vatItem.isSalesType) {
            b();
        } else {
            a();
        }
    } else {
        if (adjustment.vatItem.isSalesType) {
            a();
        } else {
            b();
        }
    }
}

剩下的 block 也是一样的,只是颠倒了a()b():

if (adjustment.adjustmentAccount.isIncrease) {
    if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
        a();
    } else {
        b();
    }
} else {
    if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
        b();
    } else {
        a();
    }
}

所以我们开始看到逻辑。如果它是增加的,并且 increaseVATLine 与 isSalesType 匹配,那么我们借记,否则记入贷方,但如果它是减少的,那么只有当它们不匹配时我们才记入贷方。表达这个的好方法是什么?好吧,首先,更聪明地命名 a() 和 b() - 现在我们可以看到它们在做什么

if (adjustment.adjustmentAccount.isIncrease) {
    if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
        debitEntry();
    } else {
        creditEntry();
    }
} else {
    if (adjustment.increaseVATLine == adjustment.vatItem.isSalesType) {
        creditEntry();
    } else {
        debitEntry();
    }
}

现在它更清晰了一点。当它是增加帐户和增加增值税行以及销售类型时,或者当它是减少并且它是减少增值税行或它是销售类型时,而不是两者。这个真值表有帮助吗?第一列是adjustmentAmount.isIncrease;第二个是adjustment.increaseVATLine;第三个是 adjustment.vatItem.isSalesType。第四列是D为借方,C为贷方;括号中是标志中 TRUE 值的数量。

TTT -> D (3) 
TFF -> D (1) 
TTF -> C (2)
TFT -> C (2) 
FTT -> C (2) 
FFF -> C (0)
FTF -> D (1) 
FFT -> D (1)

现在您可以明白为什么@Xavier Ho 的解决方案有效了;奇数都是借方,偶数都是贷方。

这只是一条探索路径;希望对您有所帮助。

关于java - 重构复杂的 if 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2772303/

相关文章:

java - Selenium 将焦点切换到选项卡,单击链接后打开该选项卡

conditional - JSTL:检查属性是否不存在

php - 哪个条件语句是正确的?

java - 静态匿名类在Java中就一定是错误的吗?

Java多次匹配同一个组

java - 如何结合 Webstart Maven 插件使用 Maven 将 'all-permissions' 添加到 list 文件?

c# - C#重构问题

java - map 中特定键的值总和

java - 重构了很多类似方法的代码

java - 使用 ANT 时,如果我有特定的 Java 版本,如何才能定义任务?