Invert guard let scoping

I try to avoid this in a couple of ways:

  1. I generally prefer using switch over if let/guard with the earlier branches handling the error paths and the final branch handling the happy path.

  2. Refactoring out the error-handling logic into a closure. This often makes it easier to spot inconsistencies in error-handling in different parts of the code.

    let handleXError = { error in
      log("ERROR: \(error)")
      completion(nil, error)
    }
    if let error = error {
      handleXError()
      return;
    }
    

    This pattern makes it easy to visually scan the error-handling if lets (they all look like handleABCError() followed by return) and double-check the returns.

Not saying this necessarily solves your problem, but something worth to try out/experimenting with if you feel like it might work for you. :slight_smile: