I try to avoid this in a couple of ways:
-
I generally prefer using
switch
overif let
/guard
with the earlier branches handling the error paths and the final branch handling the happy path. -
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 let
s (they all look likehandleABCError()
followed byreturn
) and double-check thereturn
s.
Not saying this necessarily solves your problem, but something worth to try out/experimenting with if you feel like it might work for you.