关于从嵌套语句返回的快速问题

发布于 2024-07-10 12:07:57 字数 213 浏览 10 评论 0原文

如果我有一个循环或一组 if/else 语句之类的东西,并且我想从嵌套中返回一个值(见下文),那么最好的方法是将值分配给字段或属性并返回那?

见下文:

bool b;

public bool ifelse(int i)
{
if(i == 5)
{
b = true;
}

else
{
b = false;
}
return b;
}

If I have something like a loop or a set of if/else statements, and I want to return a value from within the nest (see below), is the best way of doing this to assign the value to a field or property and return that?

See below:

bool b;

public bool ifelse(int i)
{
if(i == 5)
{
b = true;
}

else
{
b = false;
}
return b;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

在巴黎塔顶看东京樱花 2024-07-17 12:07:57

关于什么

return i == 5;

what about

return i == 5;
战皆罪 2024-07-17 12:07:57

对此有多种看法。 我认为大多数人(包括我)倾向于在得到答案并且没有更多工作要做后立即返回。 有些人会争辩说你应该只在方法的最后一个语句处返回。 然而,在某些情况下,它实际上会使事情变得更加复杂。

按照我的建议,你的例子会更短更简单:

public bool ifelse(int i)
{
if(i == 5)
{
return true
}
return false
}

There are multiple views on this. I think most people (including me) tend to prefer to return as soon as you have an answer and there is no more work to do. Some people will argue that you should only ever return at the last statement of a method. However, it can actually make things more complicated in some situations.

Going by what I've suggested, your example would be shorter and simpler:

public bool ifelse(int i)
{
if(i == 5)
{
return true
}
return false
}
水溶 2024-07-17 12:07:57

如果 b 仅用于计算方法的返回值,那么您应该将其设为局部变量(在方法内定义)。

public bool ifelse(int i)
{
  bool b;
  /*
  Some code to calculate b
  */
  return b;
}

正如其他人所建议的,如果您的方法很简单,我会完全避免使用临时变量并在知道结果后立即返回结果。 一般规则是使用使代码最容易阅读的方法。

If b is only used to calculate the return value for your method then you should make it local variable (defined within the method).

public bool ifelse(int i)
{
  bool b;
  /*
  Some code to calculate b
  */
  return b;
}

As others have suggested, If your method is simple I would avoid using a temporary variable altogether and return the result as soon as it is known. A general rule would be to use whichever method makes the code easiest to read.

会发光的星星闪亮亮i 2024-07-17 12:07:57

是的,这是很好的风格。

另一种选择(这将是)是这样做:


public bool ifelse(int i) 
{ 
    if(i == 5) 
    { 
        return true; 
    }
    else 
    { 
        return false; 
    }
}

多个返回点被认为是坏风格的原因是,特别是对于较大的方法,可能很难跟踪程序流内的程序流。方法,因为它可以在任何点退出。 这对调试来说可能是一场噩梦。 但是,如果您有一个分配给的返回变量,您可以观察该变量并确切地知道它将何时返回(从单个位置)。

情况并非总是如此,因为编程中的每个风格点都有好的一面和坏的一面。

Yes, that is good style.

The alternative (which would be bad) would be to do this:


public bool ifelse(int i) 
{ 
    if(i == 5) 
    { 
        return true; 
    }
    else 
    { 
        return false; 
    }
}

The reason multiple return points are regarded as bad style is that especially for larger methods it can be difficult to keep track of the program flow within a method because it could exit at any point. This can be a nightmare to debug. If you have a return variable that you assign to however, you can watch that variable and know exactly when it will be returned (from a single place).

This isn't always the case, as with every stylistic point in programming there are good sides to it and bad sides.

献世佛 2024-07-17 12:07:57

我想说的是,您通常应该只在两个地方从一个方法返回 - 靠近开头(如在保护条件下)和靠近结尾; 如果该方法有任何长度,您应该像您提到的那样使用临时变量,否则阅读代码的人可能会很难理解它。

I would say if you should generally only return from a method in two places - near the beginning (as in guard conditions) and near the end; If the method has any length to it, you should use a temporary variable as you mentioned, otherwise people reading the code may have a harder time following it.

放飞的风筝 2024-07-17 12:07:57

正如所指出的,拥有多个返回语句的缺点是很难找到它们。 OTOH 在某些情况下,转义到该 return 语句所需的添加逻辑比该样式正在解决的问题更糟糕。

我知道多次返回的主要问题是您可能很快就会忘记在新的返回点进行一些清理处理等。 恕我直言,这对于单一返回形式来说也是一个问题,因为转义路径必须记住包含该代码而不是其他代码。 在某些语言(如 c#)中,对此问题的一种解决方案是使用 finally 块,或者它的形式更简洁 范围声明,如此处所示。 (好的,我现在就去拿我的肥皂盒)

As pointed out, having more than one return statement has the downside of finding them gets hard. OTOH in some cases the added logic needed to escape to that return statement is worse that the problem the style is solving.

The major problem I'm aware of re multiple returns is that you can quickly forget to do some cleanup processing or the like at a new return point. IMHO this is just as much a problem with the single return form because the escape path has to remember to include that code and no other code. One solution to this, available in some languages like c#, is the finally block or it's neater form the scope statement as demonstrated here. (OK I'll get of my soap box now)

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