The nice thing about guard
is that you can tell by just looking at the condition - without looking into the else
branch - that you leave the scope here. This is not the case for if
and if let
. If you are one of those developers who want to communicate intent in that more global way, you'd have to embed the rest of the function into an else
. If you need to do this multiple times and you're unlucky enough that you need to execute some code in between those checks so that else if
is not an option, you end up with deeply nested else
s which is unreadable.
Unfortunately, support for structured programming in Swift is not much better than in most other languages. I'd love to have annotations for control-flow statements (if
, if let
, switch
, for in
, while
and while let
) to express "this thing always leaves scope", "this thing sometimes leaves scope" and "this thing never leaves scope", but in order to not be source-breaking, they'd have to be opt-in (which means either a lot of noisy warnings in old code bases or no enforcement of the annotations at all) or implicitly added and shown to the user (which would be a paradigm shift for Swift).