构建“如果”的更好方法陈述
以下哪项是构造 If
语句嵌套的更好方法。
if (x && y)
doXY();
else if (x)
doX();
else if (y)
doY();
(或者)
if(x)
if(y)
doXY();
else
doX();
else if(Y)
doY();
Which of the following is a better way to structure nesting of If
statments.
if (x && y)
doXY();
else if (x)
doX();
else if (y)
doY();
(OR)
if(x)
if(y)
doXY();
else
doX();
else if(Y)
doY();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
第一个嵌套较少,所以我这么说。
在第二个中,您正在评估“x”是否为真,然后进入该块并评估“y”是否为真,通常最好的做法是尽可能少地嵌套代码。
The first has less nesting so id say that.
In the second one you are evaluating wether "x" is true, then entering into that block and evaluating if "y" is true, its generally good practice to nest code as little as possible.
就可读性而言,我肯定会选择第一个选项。
但是,如果这是执行数百万次的逻辑的一部分,并且您知道 x 和 x 的概率分布; y 为 true 与仅 x 为 true 或仅 y 为 true 相比,倾向于后者之一,您可能希望为了性能而牺牲可读性。但是,请确保在进行此类优化之前进行分析,并确保记录了构建此类 if 语句的推理,以便其他开发人员不会进来并通过重构来帮你一个忙你的代码。
Readability-wise I would definitely go with the first option.
But if that's part of some logic that executes millions of times, and you know the distribution of probabilities for both x & y being true vs. only x being true, or only y being true, favors one of the latter, you may want to sacrifice readability for the sake of performance. However, make sure you profile before jumping to optimizations like that, and make sure you document the reasoning for structuring the if statements like that so other developers won't just come in and do you a favor by refactoring your code.
另一种可能性:
我预先声明,这效率较低,但程度极小,几乎不重要。从可读性和可维护性的角度来看,在某些情况下可能会更好,因为没有任何子句依赖于任何其他子句,并且不要求它们按指定的顺序执行。如果需要,每个单独的子句都可以提取到自己的方法中。
Another possibility:
This is - I state up front - less efficient, but to such a minuscule extent that it would hardly ever matter. From the readability and maintainability standpoint, it may be better in some circumstances, because none of the clauses depends on any of the others and there is no requirement that they be executed in the order specified. Each individual clause could be extracted into its own method, if desired.
1.) 正如 kyndigs 所说,减少嵌套是一个很好的做法。
2.) 另一个好的建议是用括号
{ }
将运算符块括起来,无论是否只有一个或多个被调用方法。3.) 始终尝试简化你的“if”语句。我的意思是
if (isTrue)
比if (!(!isNotFalse))
我会这样编写上面的代码:
1.) As kyndigs said, less nesting is a good practice.
2.) Another good advice is to surround the block of operators with brackets
{ }
no matter if there is only one called method or more.3.) Always try to simplify your "if" statement. I mean
if (isTrue)
is better thanif (!(!isNotFalse))
I would write the code above in this way:
我认为第一个更好的可读性和结构性
I think the first one is better for readability as well as well structuredness
您还可以执行以下操作:
免责声明:请注意,这既不是更快,也不是更好的可读性,只是一种替代方案,在极少数情况下可能是可接受的解决方案。
You can also do the following:
Disclaimer: Note that this is neither faster nor better readable, just an alternative, which might in rare cases be a acceptable solution.
我喜欢将代码行视为维护成本。更少的线路=更低的成本。在你的情况下,第一个代码块短了一行,所以我投票赞成。
一般来说,这当然取决于声明的上下文。
查看 SLOC 上的 Wiki 页面
I like to think of lines of code as a maintenance cost. Fewer lines = lower cost. In your case the first code block is one line shorter, so I vote for that.
Generally this would of course depend on the context of the statement.
Check out this Wiki page on SLOC
DoXY 中的操作与操作 DoX() 和 DoY() 是否互斥?即:您能否重新设计操作,使其像这样工作:
甚至可能作为 DoXY() 的参数
,但为了可读性,我可能会选择第一个选项...
Are the actions in DoXY mutually exclusive from the Actions DoX() and DoY()? Ie: can you re-work the actions to work like this:
Of maybe even as Paramters to DoXY()
But I would probably go with the first one of your options for readability...