构建“如果”的更好方法陈述

发布于 2024-09-26 12:26:42 字数 258 浏览 4 评论 0原文

以下哪项是构造 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷弦 2024-10-03 12:26:42

第一个嵌套较少,所以我这么说。

在第二个中,您正在评估“x”是否为真,然后进入该块并评估“y”是否为真,通常最好的做法是尽可能少地嵌套代码。

if (x && y)
{
   // doXY
}
else if (x)
{
   // doX
}
else
{
   // doY
}

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.

if (x && y)
{
   // doXY
}
else if (x)
{
   // doX
}
else
{
   // doY
}
囚你心 2024-10-03 12:26:42

就可读性而言,我肯定会选择第一个选项。

但是,如果这是执行数百万次的逻辑的一部分,并且您知道 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.

我的影子我的梦 2024-10-03 12:26:42

另一种可能性:

if (x && y)
   doXY();
if (x && !y)
   doX();
if (!x && y)
   doY();

我预先声明,这效率较低,但程度极小,几乎不重要。从可读性和可维护性的角度来看,在某些情况下可能会更好,因为没有任何子句依赖于任何其他子句,并且不要求它们按指定的顺序执行。如果需要,每个单独的子句都可以提取到自己的方法中。

Another possibility:

if (x && y)
   doXY();
if (x && !y)
   doX();
if (!x && y)
   doY();

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.

弃爱 2024-10-03 12:26:42

1.) 正如 kyndigs 所说,减少嵌套是一个很好的做法。

2.) 另一个好的建议是用括号 { } 将运算符块括起来,无论是否只有一个或多个被调用方法。

3.) 始终尝试简化你的“if”语句。我的意思是 if (isTrue)if (!(!isNotFalse))

我会这样编写上面的代码:

if (x && y) {
   doXY();
}
else if (x) {
   doX();
}
else if (y) {
   doY();
}

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 than if (!(!isNotFalse))

I would write the code above in this way:

if (x && y) {
   doXY();
}
else if (x) {
   doX();
}
else if (y) {
   doY();
}
后知后觉 2024-10-03 12:26:42

我认为第一个更好的可读性和结构性

I think the first one is better for readability as well as well structuredness

指尖上得阳光 2024-10-03 12:26:42

您还可以执行以下操作:

switch (x + (y<<1))
{
   case 1: doX();  break;
   case 2: doY();  break;
   case 3: doXY(); break;
}

免责声明:请注意,这既不是更快,也不是更好的可读性,只是一种替代方案,在极少数情况下可能是可接受的解决方案。

You can also do the following:

switch (x + (y<<1))
{
   case 1: doX();  break;
   case 2: doY();  break;
   case 3: doXY(); break;
}

Disclaimer: Note that this is neither faster nor better readable, just an alternative, which might in rare cases be a acceptable solution.

不语却知心 2024-10-03 12:26:42

我喜欢将代码行视为维护成本。更少的线路=更低的成本。在你的情况下,第一个代码块短了一行,所以我投票赞成。

一般来说,这当然取决于声明的上下文。

查看 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

So要识趣 2024-10-03 12:26:42

DoXY 中的操作与操作 DoX() 和 DoY() 是否互斥?即:您能否重新设计操作,使其像这样工作:

if (x) { DoX(); }
if (y) { DoY(); }
if (X && y) { DoXY(); }

甚至可能作为 DoXY() 的参数

if (X || y) { DoXY(x,y); }

,但为了可读性,我可能会选择第一个选项...

Are the actions in DoXY mutually exclusive from the Actions DoX() and DoY()? Ie: can you re-work the actions to work like this:

if (x) { DoX(); }
if (y) { DoY(); }
if (X && y) { DoXY(); }

Of maybe even as Paramters to DoXY()

if (X || y) { DoXY(x,y); }

But I would probably go with the first one of your options for readability...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文