javascript 将 if/else 放在另一个 else 中是不好的做法吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5623450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it bad practice to place an if/else inside of another else?
提问by theJava
if() {
}else {
if (IsAlternateRow=='true')
IsAlternateRow = 'false';
else
IsAlternateRow = 'true';
}
Can I place an if and else statement inside of another else statement?
我可以在另一个 else 语句中放置一个 if 和 else 语句吗?
回答by NPE
Note: the question got re-tagged as JavaScript after this answer was posted (it was originally about Java, so this answer is about Java).
注意:发布此答案后,该问题被重新标记为 JavaScript(它最初是关于 Java,所以此答案是关于 Java 的)。
In general, it is fine to place an if
and an else
inside an else
clause.
一般来说,在子句中放置 anif
和 an 是else
可以的else
。
There are, however, several issues with your code. IsAlternateRow=='true'
isn't syntactically valid.
但是,您的代码存在几个问题。IsAlternateRow=='true'
在语法上无效。
- If it's a string comparison, you should use double quotes and
.equals()
; - If it's a boolean comparison, you're better off simply doing
IsAlternateRow = !IsAlternateRow
instead of the entire nestedif
.
- 如果是字符串比较,则应使用双引号和
.equals()
; - 如果它是一个布尔比较,你最好简单地做
IsAlternateRow = !IsAlternateRow
而不是整个嵌套的if
.
回答by Ben Page
Yes, placing an if
inside an else
is perfectly acceptable practice but in most cases use of else if
is clearer and cleaner. E.g.
是的,将 anif
放入 anelse
是完全可以接受的做法,但在大多数情况下,使用 ofelse if
更清晰、更干净。例如
if (test) {
// Do something
} else if (otherTest) {
// Do something else
} else {
// Do a third thing
}
infact this is short-hand for
事实上这是简写
if (test) {
// Do something
} else {
if (otherTest) {
// Do something else
} else {
// Do a third thing
}
}
and the two should compile to almost identical programs in most situations.
在大多数情况下,两者应该编译成几乎相同的程序。
Your code example is not very clear and will not compile correctly, clearer sample code may help us to help you out.
您的代码示例不是很清楚,不会正确编译,更清晰的示例代码可能有助于我们帮助您。
回答by Timothy Groote
try
尝试
IsAlternateRow = !IsAlternateRow;
(updated to show what this would look like in your code)
(更新以显示这在您的代码中的样子)
var IsAlternateRow = false;
if(/* -- insert equation here -- */)
{
// do something
}
else
{
IsAlternateRow = !IsAlternateRow;
}
回答by PaulJWilliams
return (IsAlternateRow.equals('true')) ? 'false' : 'true'
返回 (IsAlternateRow.equals('true')) ?“假”:“真”
回答by David Weiser
In general, I'd say that to answer questions like this, you should ask yourself,
一般来说,我会说要回答这样的问题,你应该问自己,
Is this easy to read?
这容易读吗?
Shallow-nested if/else
statements aren't terrible, but once you start nesting ad nauseum, you should probably refactor.
浅嵌套if/else
语句并不可怕,但是一旦开始嵌套令人厌烦的语句,您可能应该重构。
回答by trickdev
This is fine, but there are easier ways to do what you are doing:
这很好,但有更简单的方法来做你正在做的事情:
IsAlternateRow = !IsAlternateRow
回答by Scherbius.com
Correct way to do it:
正确的做法:
if() {
} else if (IsAlternateRow=='true') {
IsAlternateRow = 'false';
}
else
{
IsAlternateRow = 'true';
}
回答by Ash Burlaczenko
Yes you can. If you want to evaluate the same object/variable several time you could use a switch statement but in many case staggered if statement will do the job just as well.
是的你可以。如果您想多次评估同一个对象/变量,您可以使用 switch 语句,但在许多情况下,交错的 if 语句也可以完成这项工作。
回答by Chris Thompson
To answer your question, yes, you can infinitely nest if/else statements. The code you supplied will compile no problem. Although given that you're talking about Java, I imagine unless that's pseudo code, it isn't going to provide the desired result.
要回答您的问题,是的,您可以无限嵌套 if/else 语句。您提供的代码编译没有问题。尽管考虑到您在谈论 Java,但我想除非那是伪代码,否则它不会提供所需的结果。