到"如果,如果,如​​果"或至QUOT;如果,否则如果,否则如果,否则" [英] To "if, if, if" or to "if, else if, else if, else"

查看:114
本文介绍了到"如果,如果,如​​果"或至QUOT;如果,否则如果,否则如果,否则"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些code对数据进行分析,并有排除基于某些标准样品。在实践中我最后写code,如:

I am writing some code for data analysis, and have to exclude samples based on some criteria. In practice I end up writing code such as:

bool Test(SampleType sample)
{
  if( ! SubTest1(sample) )
    return false;
  if( ! SubTest2(sample) )
    return false;
  if( ! SubTest3(sample) )
    return false;

  return true;
}

下面似乎等同于我:

The following seems equivalent to me:

bool Test(SampleType sample)
{
  if( ! SubTest1(sample) )
    return false;
  else if( ! SubTest2(sample) )
    return false;
  else if( ! SubTest3(sample) )
    return false;
  else 
    return true;
}

有没有在计算成本方面的差异?是否有值得商榷的preferential之一扩展性/可维护性,美学等..?

Is there a difference in terms of computing cost? Is there a arguable preferential one in terms of extendibility/maintainability, aesthetics, etc...?

我知道这可能是一个无关紧要的问题,但一旦我得到这些问题停留在我的头上,我需要找到答案。

I know this is probably an inconsequential issue, but once I get these questions stuck in my head I NEED to find the answer.

PS:如果有人关心,我的实际code为15/09可在以下发现:
http://folk.uio.no/henrikq/conf.tgz

PS: in case anyone cares, my actual code as of 15/09 can be found at the following: http://folk.uio.no/henrikq/conf.tgz

推荐答案

编译器生成相同的code两个版本。但是第一个版本是在可维护性方面更好,如果你只是第二个版本进行比较。

Compiler generates the same code for both the versions. But the 1st version is better in maintainability aspect if you compare just with the 2nd version.

遇到收益语句时,code出口;所以没有使用保持其他在即将到来的如果的。它使开发人员了解code更好。

The code exits when the return statement is encountered; so there is no use of keeping else in the upcoming if. It makes the developer understand the code better.

此外,如果这是字面code,那么你仍然可以缩小为,

Also, if this is the literal code then you can still shrink as,

bool Test(SampleType sample)
{
  return (SubTest1(sample) && SubTest2(sample) && SubTest3(sample));
}

这篇关于到"如果,如果,如​​果"或至QUOT;如果,否则如果,否则如果,否则"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆