简化嵌套循环逻辑
我在尝试简化嵌套循环中包含的一些逻辑时遇到了一些麻烦
,我在下面提供了一些伪代码,这是我正在处理的基础知识。实际代码比较复杂,包含多个嵌套循环结构。
我负责维护这段代码,我不是它的原始作者
这是一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
引入一个变量
Introduce a variable
您正在为自己设置混乱且复杂的代码。如果可能的话,请考虑 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
使用变量作为条件 1:
Use a variable for condition 1:
当 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.
即使没有完全了解上下文,我也可以说我不想成为该代码的下一个程序员。
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.
怎么样:
您还可以尝试将中断条件放入
while
条件中。How about:
You could also try to work the breaking conditions into the
while
conditions.