用许多条件重构 'if else' 链
Refactoring 'if else' chain with many conditions
我的代码看起来像这样。一个 'if else' 链,每个链都包含一长串关于何时调用该函数的条件。
if (conditionA1() || conditionA2() && conditionA3() || ... && conditionAN()) {
functionA();
}
else if (conditionB1() || conditionB2() && conditionB3() || ... && conditionBN() {
functionB();
}
...
else if (conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN()) {
functionZ();
}
代码看起来很乱,很难维护,想知道是否有好的设计模式来重构它。
如果条件都不同,那么嵌套是不可能的......但是,我发现有时更具可读性的是,如果我将每个条件(或每两个条件)放在单独的行中,例如:
if (conditionA1() ||
conditionA2() && conditionA3() ||
... &&
conditionAN()) {
functionA();
}
else if (conditionB1() ||
conditionB2() && conditionB3() ||
... &&
conditionBN() {
functionB();
}
...
else if (conditionZ1() ||
conditionZ2() && conditionZ3() ||
... && conditionZN()) {
functionZ();
}
这里看起来有点滑稽,但通常如果我用实际条件来做它看起来会更好:)
我知道两个处理长链条件的好技巧。
首先是创建命名变量来描述组合条件。
firstCompoundCondition = conditionA1() || conditionA2() && conditionA3() || ... && conditionAN();
secondCompoundCondition = conditionB1() || conditionB2() && conditionB3() || ... && conditionBN();
...
nthCompoundCondition = conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN();
if (firstCompoundCondition) {
functionA();
}
else if (secondCompoundCondition) {
functionB();
}
...
else if (nthCompoundCondition) {
functionZ();
}
第二个 - 这有点微妙,也许最终更强大 - 是通过将代码结构化为 return 来消除对所有 else
的需要,理想情况下 returning 内部函数的结果,但简单地 returning 在块内也可以工作。这可能意味着提取一个看起来不太像的函数,但当一切都说完之后,它就干净多了。
firstCompoundCondition = conditionA1() || conditionA2() && conditionA3() || ... && conditionAN();
secondCompoundCondition = conditionB1() || conditionB2() && conditionB3() || ... && conditionBN();
...
nthCompoundCondition = conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN();
if (firstCompoundCondition) return functionA();
if (secondCompoundCondition) return functionB();
...
if (nthCompoundCondition) return functionZ();
我的代码看起来像这样。一个 'if else' 链,每个链都包含一长串关于何时调用该函数的条件。
if (conditionA1() || conditionA2() && conditionA3() || ... && conditionAN()) {
functionA();
}
else if (conditionB1() || conditionB2() && conditionB3() || ... && conditionBN() {
functionB();
}
...
else if (conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN()) {
functionZ();
}
代码看起来很乱,很难维护,想知道是否有好的设计模式来重构它。
如果条件都不同,那么嵌套是不可能的......但是,我发现有时更具可读性的是,如果我将每个条件(或每两个条件)放在单独的行中,例如:
if (conditionA1() ||
conditionA2() && conditionA3() ||
... &&
conditionAN()) {
functionA();
}
else if (conditionB1() ||
conditionB2() && conditionB3() ||
... &&
conditionBN() {
functionB();
}
...
else if (conditionZ1() ||
conditionZ2() && conditionZ3() ||
... && conditionZN()) {
functionZ();
}
这里看起来有点滑稽,但通常如果我用实际条件来做它看起来会更好:)
我知道两个处理长链条件的好技巧。
首先是创建命名变量来描述组合条件。
firstCompoundCondition = conditionA1() || conditionA2() && conditionA3() || ... && conditionAN();
secondCompoundCondition = conditionB1() || conditionB2() && conditionB3() || ... && conditionBN();
...
nthCompoundCondition = conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN();
if (firstCompoundCondition) {
functionA();
}
else if (secondCompoundCondition) {
functionB();
}
...
else if (nthCompoundCondition) {
functionZ();
}
第二个 - 这有点微妙,也许最终更强大 - 是通过将代码结构化为 return 来消除对所有 else
的需要,理想情况下 returning 内部函数的结果,但简单地 returning 在块内也可以工作。这可能意味着提取一个看起来不太像的函数,但当一切都说完之后,它就干净多了。
firstCompoundCondition = conditionA1() || conditionA2() && conditionA3() || ... && conditionAN();
secondCompoundCondition = conditionB1() || conditionB2() && conditionB3() || ... && conditionBN();
...
nthCompoundCondition = conditionZ1() || conditionZ2() && conditionZ3() || ... && conditionZN();
if (firstCompoundCondition) return functionA();
if (secondCompoundCondition) return functionB();
...
if (nthCompoundCondition) return functionZ();