java 两者中哪个更好:if-elif-else 或 if-else
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16396330/
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
Which is better of the two: if-elif-else or if-else
提问by Harshdeep
This is not a homework question, this question is a result of just another discussion with my friend.
这不是作业问题,这个问题只是与我的朋友再次讨论的结果。
I searched a lot but could not find any reliable source which establishes the answer with proper reasoning so here I am at the mecca of programmers.
我搜索了很多,但找不到任何可靠的来源,可以通过适当的推理建立答案,所以我来到了程序员的圣地。
Which of the following two ways of coding is better:
以下两种编码方式哪个更好:
if(a)
{
if(b)
....
else if(c)
....
}
or
或者
if(a && b)
...
else if(a && c)
...
I searched and found http://en.wikipedia.org/wiki/Cyclomatic_complexityto be useful but still could not make out final choice.
我搜索并发现http://en.wikipedia.org/wiki/Cyclomatic_complexity很有用,但仍然无法做出最终选择。
Please provide relevant reasoning as well.
也请提供相关的理由。
Cheers!
干杯!
回答by Koushik Shetty
I think without optimization first method should be faster.
我认为没有优化的第一种方法应该更快。
Since in 1st method you check a
onceand then its b
or c
. if b is false
then there are 3 checks(comparisons).
由于在第一种方法中您检查a
一次,然后检查其b
or c
。如果 b 是,false
则有 3 个检查(比较)。
But in the second you check a
twiceif b
is false
(once if b
is true
). There will be 4 checks if b
is false. but the compiler will probably optimize both to be the same.
但是在第二个你检查a
两次if b
is false
(一次 if b
is true
)。如果b
为假,将有 4 次检查。但编译器可能会将两者优化为相同。
回答by Thang Nguyen
In my coding experience, it depends on the content of the dots. If just a single or few lines of code, I prefer the 2nd. However, if there're more, I'll use 1st block.
在我的编码经验中,这取决于点的内容。如果只有一行或几行代码,我更喜欢第二行。但是,如果有更多,我将使用第一个块。
回答by vlad_tepesch
obsolete due to question changes
由于问题更改而过时
that may not be the same since the if(b) block may change the a.
这可能不一样,因为 if(b) 块可能会改变 a。
If all variables in the conditions are constant then the compiler may output the same code to avoid the additional andoperation of the second variant.
如果条件中的所有变量都是常量,那么编译器可能会输出相同的代码,以避免第二个变量的附加和操作。
The additional & is also the reason why i would prefer the first variant.
额外的 & 也是我更喜欢第一个变体的原因。
回答by Tralli
The first one possibly makes fewer comparison since it compares a only once, but the second one always compares a twice and b and c once
第一个可能比较少,因为它只比较 a 一次,但第二个总是比较 a 两次,b 和 c 一次
回答by Nicolas Manzini
Obviously if (a) ... then if b... You can think of it as distribution.. a*b + a*c = a*(b+c) or tree building. yes - no - no, yes - no - yes and so on. You know your logic is good when you can build easily that three otherwise you must rethink your logic.
显然,如果 (a) ... 那么如果 b ... 您可以将其视为分布.. a*b + a*c = a*(b+c) 或树构建。是 - 不 - 不,是 - 不 - 是等等。当您可以轻松构建这三个时,您就知道您的逻辑很好,否则您必须重新思考您的逻辑。
回答by amin k
Almost they are not Difference in time order or any thinks. Difference between them is completely relation to your architect of your code and your vision.for example if condition of A is repeater i suggest first way,else i prefer a simple code like as second way because this is easy for read and continues.and other development is More enthusiastic to help you.
他们几乎没有时间顺序上的差异或任何想法。它们之间的区别完全与您的代码架构师和您的愿景有关。例如,如果 A 的条件是中继器,我建议第一种方式,否则我更喜欢像第二种方式这样的简单代码,因为这易于阅读和继续。和其他发展更热心助您一臂之力。