哪种方法最好返回true或false? [英] which approach is better to return true or false?
问题描述
也许我的问题并不重要,但是每当我从事项目工作时,它就会在我脑海中浮现,所以我想知道哪种方法在性能方面更好,我还想知道还有其他方法吗?副作用.
May be my question is not important but It pop up in my mind whenever I do work on my project,So I want to know that which one is better approach in terms of performance and I also want to know are there any other side effects.
代码1:
if ($result === TRUE){
// some statements
return TRUE;
}else{
// some statements
return FALSE;
}
代码2:
if ($result === FALSE){
// some statements
return FALSE;
}
// some statements
return TRUE;
请注意,当条件为true或false时,我必须处理一些代码,这就是为什么我在注释中声明"//一些语句"
Please note that I have to process some code when condition is true or false that's why I putted comment stating that "// some statements"
推荐答案
两者的性能相同. 选择一个更好看的书,第一个可以使您获得V形表,如
The performance of both is the same. Pick the one that's better to read, the first one can lead you to get a V Form like
if($result) {
} else {
for() {
if($result2) {
//code
}
}
}
所以我更喜欢第二个,对于高级程序员而言,它更容易阅读,但对初学者来说有点困惑
so i would prefer the second one, which is much easier to read for advanced programmers, but a little confusing to beginners
这篇关于哪种方法最好返回true或false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!