基于两个布尔变量的分支
假设我有两个布尔变量,并且我想根据它们的值做完全不同的事情。实现这一目标的最干净的方法是什么?
变体 1:
if (a && b)
{
// ...
}
else if (a && !b)
{
// ...
}
else if (!a && b)
{
// ...
}
else
{
// ...
}
变体 2:
if (a)
{
if (b)
{
// ...
}
else
{
// ...
}
}
else
{
if (b)
{
// ...
}
else
{
// ...
}
}
变体 3:
switch (a << 1 | b)
{
case 0:
// ...
break;
case 1:
// ...
break;
case 2:
// ...
break;
case 3:
// ...
break;
}
变体 4:
lut[a][b]();
void (*lut[2][2])() = {false_false, false_true, true_false, true_true};
void false_false()
{
// ...
}
void false_true()
{
// ...
}
void true_false()
{
// ...
}
void true_true()
{
// ...
}
变体 3 和 4 是否过于棘手/复杂普通程序员?我错过了任何其他变体吗?
Suppose I have two boolean variables, and I want to do completely different things based on their values. What is the cleanest way to achieve this?
Variant 1:
if (a && b)
{
// ...
}
else if (a && !b)
{
// ...
}
else if (!a && b)
{
// ...
}
else
{
// ...
}
Variant 2:
if (a)
{
if (b)
{
// ...
}
else
{
// ...
}
}
else
{
if (b)
{
// ...
}
else
{
// ...
}
}
Variant 3:
switch (a << 1 | b)
{
case 0:
// ...
break;
case 1:
// ...
break;
case 2:
// ...
break;
case 3:
// ...
break;
}
Variant 4:
lut[a][b]();
void (*lut[2][2])() = {false_false, false_true, true_false, true_true};
void false_false()
{
// ...
}
void false_true()
{
// ...
}
void true_false()
{
// ...
}
void true_true()
{
// ...
}
Are variants 3 and 4 too tricky/complicated for the average programmer? Any other variants I have missed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个变体是最清晰、最易读的,但可以调整:
The first variant is the clearest and most readable, but it can be adjusted:
您忘记了:
当适用且您确实需要 4 种不同的行为时,这可能是最佳选择。
如果你有超过 2 个布尔值,如果我有 2^n 种不同的行为,而这些行为不能像上面那样很好地分解,我会将其视为代码味道。然后我可能会考虑这样做:
但是没有上下文,很难判断这样的复杂性是否有必要。
You forgot about:
which is probably the best choice when appliable, and when you truly need 4 different behaviours.
If you have more than 2 bools, I take this as a code smell if I have 2^n different behaviours which don't factorize well like the above. Then I may think about doing:
but without context, it is hard to tell whether such complexity is necessary.
恕我直言,我会选择
变体3
。因为就我个人而言,当我检查相等性时,我不喜欢if/else
。它明确指出只有4种可能性。一个小的修改是:
为了使其更奇特,您也可以将
0,1,2,3
替换为enum
。IMHO, I will go for
variant 3
. Because personally, I don't likeif/else
when I am checking for equality. It clearly states that there are only 4 possibilities.One minor edit would be:
To make it more fancy, you may replace
0,1,2,3
with anenum
as well.对于只有两个布尔值的情况,其中任何一个都是好的且合理的。大家可以根据自己的口味来选择。
但是,如果有两个以上的布尔值,例如四个布尔值,那么我个人会使用查找表,并且我会这样做:
MakeInt()
很容易写:For just two booleans, any of them is good and reasonable. One can choose based on his taste.
However, if there are more than two booleans, say four booleans, then I personally would go with lookup table, and I would do this as:
MakeInt()
is easy to write: