首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >重构复杂的if条件

重构复杂的if条件
EN

Stack Overflow用户
提问于 2010-05-05 18:21:44
回答 10查看 2.5K关注 0票数 7

有没有人能建议最好的方法来避免大多数if条件?我有下面的代码,我想避免大多数情况下,如果条件,怎么做?任何解决方案都是非常有帮助的;

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2010-05-05 18:38:16

我想这行得通。我基本上推广了你的布尔逻辑。下一次,试着画一些图表来帮助你理清思路。

我想从这篇文章的评论中指出,Marcelo和BlueRaja提供的异或解决方案在功能上是相同的。

代码语言:javascript
复制
/* This is to avoid a crazy 3-way switch. Generalised.
 * Instead of using a complicated if-else branch, we can use the number of true
 * and false to entail the intended action. */
/* This is the same as a ^ b ^ c (chained XOR), 
 * which is used to count the parity of truth values. */
int a = adjustment.adjustmentAccount.isIncrease ? 1 : 0;
int b = adjustment.increaseVATLine ? 1 : 0;
int c = adjustment.vatItem.isSalesType ? 1 : 0;

if ((a + b + c) % 2 == 1)
{
    entry2.setDebit(adjustment.total);          // Odd number of trues
    entry2.setCredit(0d);
}
else
{
    entry2.setCredit(adjustment.total);         // Even number of trues
    entry2.setDebit(0d);
}
票数 6
EN

Stack Overflow用户

发布于 2010-05-06 01:06:41

该怎么做呢...让我们提取几个方法,以便更好地了解其中的逻辑。

代码语言:javascript
复制
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();
    }
}

所以现在,看着它,第一块

代码语言:javascript
复制
if (adjustment.increaseVATLine) {
    if (adjustment.vatItem.isSalesType) {
        a();
    } else {
        b();
    }
} else {
    if (adjustment.vatItem.isSalesType) {
        b();
    } else {
        a();
    }
}

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

代码语言:javascript
复制
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();
        }
    }
}

剩下的块是相同的,只是颠倒了a()b()

代码语言:javascript
复制
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()更聪明的名字--现在我们可以看到他们在做什么了

代码语言:javascript
复制
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表示贷方;括号中是标志中真值的数量。

代码语言:javascript
复制
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的解决方案是有效的了;奇数总数都是借方,偶数总数都是贷方。

这只是一条探索性的道路;我希望它能有所帮助。

票数 18
EN

Stack Overflow用户

发布于 2010-05-05 18:32:23

我还没有彻底验证这个逻辑,但这是基本的想法:

代码语言:javascript
复制
amt = adjustment.total
if (adjustment.adjustmentAccount.isIncrease
    ^ adjustment.increaseVATLine
    ^ adjustment.vatItem.isSalesType)
{
    amt = -amt;
}

entry2.setCredit(amt > 0 ? amt : 0);
entry2.setDebit(amt < 0 ? -amt : 0);

我应该注意到,这个逻辑略有不同,因为它正确地处理了adjustment.total的负值,而原来的逻辑似乎假定(可能是正确的)该值始终为非负值。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2772303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档