Refactor the goto

KingKaeru

Supreme [H]ardness
Joined
Jul 16, 2000
Messages
6,465
Here's a little exercise for you guys. How would you refactor this chunk of c# code to lose the goto? :)

Do you think the goto is warranted in this case (short circuiting a nested foreach) or is there a better design pattern to use here?

Code:
foreach (Item item in Items)
{
   foreach (Token token in tokens)
   {
      if(item.isRelatedTo(token))
      {
         success = process(token, item);
         goto processDone;
      }
   }
}

processDone:
   if(!success) defaultProcessing();
 
Code:
boolean success = null;
foreach (Item item in Items)
{
   foreach (Token token in tokens)
   {
      if(item.isRelatedTo(token))
      {
         success = process(token, item);
         break;
      }
   }
  if (success != null) break;
}
if(!success) defaultProcessing();

I don't know C#, but, something like that would do it. Of course, that code isn't as efficient as your original code. Plus, the assembler is going to create jumps anyway, this seems like a perfectly valid case for a goto.

Without knowing exactly what you're doing, I'm not sure anyone could give you advice on a more efficient way of comparing the items. Is there any way that the tokens could be sorted in a way that would make comparing them easier? What does .isRelatedTo() do to the token?

If the method is comparing a string value, like a UPC or similar, to a property of the item. One could probably store a sorted list of all the token comparison values, then use a binary search on the list for each item. This would reduce complexity to nlog(m). Of course, that's only if my assumption is valid.
 
Last edited:
It sounds like what you're looking for is an alternative to break out of a nested loop using goto that doesn't involve too many extra lines.

Code:
foreach (Item item in Items)
{
   foreach (Token token in tokens)
   {
      if(item.isRelatedTo(token))
      {
         success = process(token, item);
         break;
      }
   }
   if(success!=null)
       break;
}
if(!success)
   defaultProcessing();

That's the quick and dirty way of removing your goto statement, assuming that you initialized success as null.

Java gives you the ability to name loops... I'm not sure if C# allows you to do the same. With Java you could name your outer loop as mainloop, then say

Code:
break mainloop;
That break statement would break out of the outer loop, thus taking you to the if statement to determine whether or not to use defaultProcessing.
 
I don't know C# but this might be another way:

Code:
function process(){
  foreach (Item item in Items)
  {
    foreach (Token token in tokens)
    {
      if(item.isRelatedTo(token))
      {
        success = process(token, item);
        return success;
      }
    }
  }
  return false;
}
function main()
{
  if(!process())
  {
    defaultProcessing();
  }
}
 
if you are looking for something quite different, you could do all sorts of crazy stuff. See the following assuming you are using generic collections, etc.

Code:
Token token = null;
IEnumerator<Item> item = Items.GetEnumerator();
while(token == null && item.MoveNext)
{
    token = Tokens.Find(delegate(Token t) { return item.Current.IsRelatedTo(t); });
}

if (token == null || !process(token, item.Current))
{
    defaultProcessing();
}
 
use the function return as Sgraffite said.

As for which is more efficient: don't worry about it.
 
As for which is more efficient: don't worry about it.

Well, I'd say that depends on how big those lists will be. If there are a few hundred per list, it probably isn't worth worrying about, but a few million per list, that could be slow.
 
Back
Top