0

I need to match items in two different arrays (one with imported items and another with local items that share some properties with the imported items) to sync two databases that are quite different. I need to use several criteria to do the matching to increase the robustness of finding the right local item and match it with the imported item. I could check each criterium in the same loop, but that is too expensive, because the criteria are checked by the likelihood of success in descending order. Thus, in my first implementation I used a boolean flag called found to flag that the checking of other criteria should be ignored.

Using pseudo code:

// calling code for the matching

for item in importedItems {
item.match() }

In the imported item class:

match()
{
    var found = false
    for localItem in localItems
    {
        if (self.property == localItem.property)
        {
            // update the local item here    
            found = true
            break
        }
    }

    // match with less likely 2nd property
    if (!found)
    {
         for localItem in localItems
         {
              if (self.property2 == localItem.property2)
              {
                   // update the local item here    
                    found = true
                   break
              }
          }
     }

The if !found {...} pattern is repeated two additional times with even less likely criteria.

After reviewing this code, it is clear that this can be optimized by returning instead of breaking when there is a match.

So, my question is "are there any known side-effects of leaving a loop early by using return instead of break in Swift?" I could not find any definitive answer here in SO or in the Swift documentation or in blogs that discuss Swift flow control.

jvarela
  • 3,744
  • 1
  • 22
  • 43

2 Answers2

1

If you know for sure that you can return because nothing else have to be done after the loop then there are no side effects of using return

kemkriszt
  • 280
  • 3
  • 17
  • Thank you for the answer. I just asked because I was once bitten by the compiler when I was using C/C++ and definitely there were side-effects, but I did not take the time to debug them and I just edited the code to use break and a flag and problem solved. Probably I ran into a compiler bug or a very quirky bug of mine. That is why I started to prefer break statements and flags. But in this case, it seems really wasteful. – jvarela Dec 01 '16 at 14:59
1

No, there are no side effects, quite the opposite it's more efficient.
It's like Short-circuit evaluation in a boolean expression.

But your code is a bad example because found cannot be used outside the function.

This is a more practical example returning a boolean value

func match() -> Bool
{
    for localItem in localItems
    {
        if (self.property == localItem.property)
        {
            // update the local item here    
            return true
        }
    }
   ....


   return false
}
vadian
  • 274,689
  • 30
  • 353
  • 361
  • I agree that returning a Bool value would be better, but the way the local item is updated is different for each property matched. That is why I chosen not to return the boolean value and do the update inside the matching function. – jvarela Dec 01 '16 at 14:51