简化嵌套循环逻辑

发布于 2024-10-18 21:44:31 字数 437 浏览 7 评论 0原文

我在尝试简化嵌套循环中包含的一些逻辑时遇到了一些麻烦

,我在下面提供了一些伪代码,这是我正在处理的基础知识。实际代码比较复杂,包含多个嵌套循环结构。

我负责维护这段代码,我不是它的原始作者

这是一个 goto 有效的地方吗?

while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break CONDITION2

    if(CONDITION4)
      break CONDITION
  }
}

执行这些代码中断有很多复杂性。

我不允许使用 goto,但我认为这将是我目前正在尝试做的更简单的解决方案。我应该尝试证明在这种情况下使用 goto 的合理性吗? 谢谢普拉

尚特:)

I am having some trouble trying to simplify some logic that is contained in nested loops

I have provided some pseudo code below, that is the basics of what I'm dealing with. The actual code is more complex and contains multiple nested loop structures.

I'm in charge of maintaining this code, I'm not the original writer of it

Would this be a place that a goto is valid.

while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break CONDITION2

    if(CONDITION4)
      break CONDITION
  }
}

There are many complexities to preforming these code breaks.

I am not allowed to use a goto, but I think it would be an easier solution that what I am currently trying to do. Should I try and justify the use of a goto in this case.
thanks

PRASHANT :)

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

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

发布评论

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

评论(6

━╋う一瞬間旳綻放 2024-10-25 21:44:31

引入一个变量

bool breakOuter = false;
while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break;

    if(CONDITION4)
    {
      breakOuter = true;
      break;
    }
  }
  if(breakOuter)
    break;
}

Introduce a variable

bool breakOuter = false;
while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break;

    if(CONDITION4)
    {
      breakOuter = true;
      break;
    }
  }
  if(breakOuter)
    break;
}
木槿暧夏七纪年 2024-10-25 21:44:31

您正在为自己设置混乱且复杂的代码。如果可能的话,请考虑 LINQ(循环本身实际上是一种代码味道,尤其是复杂的循环),否则请考虑将内部循环重构为单独的方法并使用 return;

编辑:Eric Lippert 的精彩帖子批评了此问题的各种替代方案:http://blogs.msdn.com/b/ericlippert/archive/2010/01/11/continuing-to-an-outer-loop.aspx

You're setting yourself up for messy and complicated code. Consider LINQ if possible (loops themselves are practically a code smell these days, especially complicated ones), otherwise consider refactoring the inner loop into a separate method and using return;.

Edit: great post by Eric Lippert critiquing the various alternatives for this problem: http://blogs.msdn.com/b/ericlippert/archive/2010/01/11/continuing-to-an-outer-loop.aspx

桃扇骨 2024-10-25 21:44:31

使用变量作为条件 1:

bool condition1 = true;
while (CONDITION && condition1)
{
    while (CONDITION2)
    {
        if (CONDITION3)
            break;
        if (CONDITION4)
        {
            condition1 = false;
            break;
        }
    }
}

Use a variable for condition 1:

bool condition1 = true;
while (CONDITION && condition1)
{
    while (CONDITION2)
    {
        if (CONDITION3)
            break;
        if (CONDITION4)
        {
            condition1 = false;
            break;
        }
    }
}
桜花祭 2024-10-25 21:44:31

当 Java(不是 C#,但相关)决定禁止 goto 时,他们通过代码分析发现 90% 的 goto 用于循环,其余的则用于非常糟糕的事情。

Break 和 continue 仍然存在,因为它们是合法的、有效的本地化 goto。确保记录他们的去向和原因,但它们仍然非常有用。

您似乎正在以最有效的方式使用它们。

When Java (not C#, but relevant) made the decision to disallow goto, they found through code profiling that 90% of all gotos were used for loops, and the rest were used for really bad stuff.

Break and continue still exist because they're legitimate, localized gotos that work. Make sure to document where they go and why they do it, but they're still very useful.

You appear to be using them in the most effective way.

爱情眠于流年 2024-10-25 21:44:31

即使没有完全了解上下文,我也可以说我不想成为该代码的下一个程序员。

LINQ 或许可以简化一些。如果您使用 ReSharper,它会在可以的情况下为您推荐。

除此之外,我会更多地思考如何首先处理这些嵌套循环,并尝试摆脱尽可能多的循环。更好的对象模型或巧妙地使用模式可以为您带来更好且更易于维护的解决方案。

Even without fully knowing the context, I can say I would hate to be the next programmer on that code.

LINQ could probably simplify it some. If you're using ReSharper, it will suggest it for you if it can.

Beyond that, I would put some more thought into how I was getting to these nested loops in the first place and try to get rid of as many as possible. A better object model or a smart use of patterns could get you a better and more maintainable solution.

守望孤独 2024-10-25 21:44:31

怎么样:

while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break CONDITION2

    if(CONDITION4)
      break CONDITION2
  }
  if(CONDITION4)
    break CONDITION
}

您还可以尝试将中断条件放入 while 条件中。

How about:

while(CONDITION)
{
  while(CONDITITION2)
  {
    if(CONDITION3)
      break CONDITION2

    if(CONDITION4)
      break CONDITION2
  }
  if(CONDITION4)
    break CONDITION
}

You could also try to work the breaking conditions into the while conditions.

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