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 🙂
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