关于if块的情况的排他性 [英] About the exclusiveness of the cases of an if block
问题描述
我对良好的编码习惯有疑问.我了解执行 if-else if
和多个 if
(即在 if-else if
,其余的检查将被跳过).我发现了以下几段代码:
I have a question about good coding practices. I understand the differences between doing an if-else if
and multiple if
s (that is, when a condition is met in an if-else if
, the rest of the checks are skipped). I've found a piece of code along these lines:
if (A == 5) {
do_something();
} else if (B == 7) {
do_something_else();
}
我了解,如果A == 5,此代码将不会检查B ==7.该代码有效,这意味着如果A不是5,则B仅是7,但是我认为这只是在等待打破当代码更改时.我会做的是:
I understand that this code won't check B == 7 if A == 5. The code works, so that means that B is only 7, if A is not 5, but I think this is just waiting to break when the code changes. What I would do is:
if (A == 5) {
do_something();
return or continue or break;
}
if (B == 7) {
do_something_else();
return or continue or break;
}
我的问题是,当我有多个依赖于不同排他变量的排他案例时,解决流程控制的最佳方法是什么?我的印象是,第一个代码(与其他ifs一起)在很大程度上取决于其他代码段,而其他区域的更改可能会破坏它.第二个似乎有点笨拙.开关可能是第三个选择,但是我需要创建另一个结构来容纳案例和分配其值的逻辑,而且我认为这有点笨拙且违反直觉.
My question is, when I have multiple exclusive cases that depend on different, exclusive variables, what's the best way to tackle the flow control? I have the impression that the first code (with else ifs) depends a lot on other pieces of code to work, and that changes in other areas might break it. The second one seems to be a bit clunky. A switch could be a third option, but I would need to create another structure to hold the case and the logic to assign its value, and I think that it would be a bit clunky and counter-intuitive.
推荐答案
您询问了排他性"案例,但条件为 A == 5
和 B == 7的问题
是它们不是 排他性的;他们是独立的.
You asked about "exclusive" cases, but the issue with the conditions A == 5
and B == 7
is that they are not exclusive; they are independent.
为全面起见,您可能需要测试和处理所有四种情况:
For full generality you may need to test and handle all four cases:
if(A == 5) {
if(B == 7) {
/* case 1 */
} else {
/* case 2 */
}
} else {
if(B == 7) {
/* case 3 */
} else {
/* case 4 */
}
}
这是臭名昭著的繁忙" if/else阻止.这是臭名昭著的,因为读者几乎无法立即跟随它,特别是在涉及案例或引入更多层次的情况下.(我认为大多数样式指南都会告诉您,永远不要使用3级或3级以上的if/else树.我肯定会这么说.)
This is the notorious "bushy" if/else block. It's notorious because it can almost immediately become nearly impossible for a reader to follow, especially if the cases are involved, or more levels are introduced. (I think most style guides will tell you never to use an if/else tree that's 3 or more levels deep. I'd certainly say that.)
我偶尔使用了这两种选择:
I have occasionally used these two alternatives:
(1)完全分离情况:
if(A == 5 && B == 7) {
/* case 1 */
} else if(A == 5 && B != 7) {
/* case 2 */
} else if(A != 5 && B == 7) {
/* case 3 */
} else if(A != 5 && B != 7) {
/* case 4 */
} else {
/* can't happen */
}
这里的目的是让以后的读者最大程度地清楚地知道案例1、2、3和4符合哪些条件.因此,您最好列出最后一个 else if(A!= 5&& B!= 7)
显式区分大小写(如我所示),即使到那时它基本上是"else".
The point here is to make it maximally clear to a later reader exactly which conditions go with cases 1, 2, 3, and 4. For this reason, you might as well list the last, else if(A != 5 && B != 7)
case explicitly (as I've shown), even though by that point it's basically an "else".
(2)设计一个两级"开关.我不能说这是一种常见的技术.它有点太聪明"的味道,但它的方式健壮且易读:
(2) Contrive a "two level" switch. I can't say this is a common technique; it has a whiff of being "too clever", but it's robust and readable, in its way:
#define PAIR(b1, b2) (((b1) << 8) | (b2))
switch(PAIR(A == 5), (B == 7)) {
case PAIR(TRUE, TRUE):
/* case 1 */
break;
case PAIR(TRUE, FALSE):
/* case 2 */
break;
case PAIR(FALSE, TRUE):
/* case 3 */
break;
case PAIR(FALSE, FALSE):
/* case 4 */
break;
}
当条件为 A == 5
和 B == 7
时,我不建议这样做,因为当您陷入困境时,这并不明显"TRUE"(真)和"FALSE"(假)的含义是什么,但是有时候,这种东西可以看得很清楚.与忙碌的" if/else树不同,它完全可以进行3个或更多级别的嵌套,正如我所说的,这是众所周知的.
I wouldn't recommend this when the conditions are A == 5
and B == 7
, because when you're down in the switch, it's not obvious what "TRUE" and "FALSE" mean, but sometimes, this sort of thing can read cleanly. It's also cleanly amenable to 3 or more levels of nesting, unlike "bushy" if/else trees, which as I said are notoriously unreadable.
这篇关于关于if块的情况的排他性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!