EDIT: Thanks for clarifying the question. The question is now really one of style and comes down to what is most readable. Generally speaking the latter is nicer when you have a true multi-way conditional, meaning at some point in the code there are three possible conditions all equally likely and equally sensible. The former is cleaner perhaps when semantically cond1 is the real driver, and cond2 is a kind of subordinate condition that people only think about when cond1 is true.
OLD ANSWER:
Here is one interpretation of the question ("nesting inside of then"):
if (cond1) {
if (cond2) {
A
}
} else {
B
}
This should be written, as Justin points out, as
if (cond1 && cond2) {
A
} else {
B
}
unless there is code that needs to be in the if cond1 part that is not in the scope of the if cond2.
Another interpretation of your question ("nesting inside of else") is:
if (cond1) {
A
} else {
if (cond2) {
B
}
}
In this case you should rewrite as
if (cond1) {
A
} else if (cond2) {
B
}
Because it better expresses the idea of the multiway conditional. Again if there is something that belongs in the scope of not cond1 but not in the scope of the cond2, then you need to nest.
(a) 中的 else 是与第一个 if 语句还是第二个 if 语句一起使用?顺便说一句,您缩进的方式,我假设它与第一个 if 语句一致,但如果是这样,那么您的两个示例在逻辑上并不相同。
Edit: In that case, I don't think either is necessarily better. In my opinion, (b) is much easier to read and follow logically. Either one will do though!
Does the else in (a) go with the first if statement or the second? By the way you indented, I would assume that it goes with the first if statement, but if that is the case, then your two examples aren't the same logically.
发布评论
评论(4)
编辑:感谢您澄清问题。现在的问题实际上是一种风格,归结为什么是最具可读性的。一般来说,当您拥有真正的多路条件时,后者更好,这意味着在代码中的某个时刻,存在三种可能的条件,它们都同样可能且同样合理。当语义上 cond1 是真实驱动程序时,前者可能更清晰,而 cond2 是一种从属条件,人们只有在 cond1 为真时才会考虑。
旧答案:
这是对该问题的一种解释(“嵌套在 then 内部”):
正如 Justin 指出的那样,这应该写成
除非有代码需要位于
if cond1
部分不在if cond2
的范围内。您的问题的另一种解释(“嵌套在 else 内部”) ”)是:
在这种情况下你应该重写为
因为它更好地表达了多路条件的思想。同样,如果有一些内容不属于 cond1 的范围但不属于 cond2 的范围,那么您需要嵌套。
希望有帮助。
EDIT: Thanks for clarifying the question. The question is now really one of style and comes down to what is most readable. Generally speaking the latter is nicer when you have a true multi-way conditional, meaning at some point in the code there are three possible conditions all equally likely and equally sensible. The former is cleaner perhaps when semantically cond1 is the real driver, and cond2 is a kind of subordinate condition that people only think about when cond1 is true.
OLD ANSWER:
Here is one interpretation of the question ("nesting inside of then"):
This should be written, as Justin points out, as
unless there is code that needs to be in the
if cond1
part that is not in the scope of theif cond2
.Another interpretation of your question ("nesting inside of else") is:
In this case you should rewrite as
Because it better expresses the idea of the multiway conditional. Again if there is something that belongs in the scope of not cond1 but not in the scope of the cond2, then you need to nest.
Hope that helps.
事实上,你的例子并不匹配。我会将第一个重写为:
像您的第一个示例那样嵌套 if 语句的唯一一次是:
Actually, your examples don't match. I would rewrite the first to be:
The only time I would nest if statements like your first example would be:
编辑:在这种情况下,我认为两者都不一定更好。在我看来,(b) 更容易阅读并遵循逻辑。不管是哪一个都可以!
(a) 中的 else 是与第一个 if 语句还是第二个 if 语句一起使用?顺便说一句,您缩进的方式,我假设它与第一个 if 语句一致,但如果是这样,那么您的两个示例在逻辑上并不相同。
Edit: In that case, I don't think either is necessarily better. In my opinion, (b) is much easier to read and follow logically. Either one will do though!
Does the else in (a) go with the first if statement or the second? By the way you indented, I would assume that it goes with the first if statement, but if that is the case, then your two examples aren't the same logically.
例子 :
Example :