Swift’s guard keyword. Why do We use it?

Recall the concern

I don’t recall exactly the first time I used Swift. Maybe since mid-2016. At these days, I was a Swift beginner. Everything was quite new to me. When I joined the project, I quickly learned, I quickly developed some parts of a video player module in Swift, in the first 2 weeks. Everything was fine.

I was familiar with objective-c before. But in Swift, there were some different concepts. For example variable type, the type can be required or optional. I believe who was an objective-c geek, would have a hard time to deal with this.

Sometimes the variable is optional, sometimes it is required. We may usually face up the crash due to force unwrapping issue. To deal with the issue, We usually use one of these statements

var optionalString : String?
if let theString = optionalString {
//do something


var optionalString : String?
guard let theString = optionalString else {
//do something with theString

Basically, both ways handle unwrapping optional type. “if” keyword is enough to handle, why is “guard” available?

Have you had the same question? If YES, then read on.

Do you know this?

That’s because of golden path (or happy path). In the scope of a function implementation, the Golden path is the primary scenario or positive scenario which everything works as expected. No errors, no exceptions at all. Any other cases belong to alternate paths, exception paths, etc.

The code belongs to the golden path should belong to left side margin, otherwise, they are on right side margin.

Let’s take a look at two pieces of code below for more details.

func authenticateUser(_ username: String?, with password: String?) -> Bool {
if let username = username, let password = password {
//Validate usernane and password
return true //Assuming, the username and password are valid
} else {
//Base input, do something
return false

In the first piece of code, We don’t see the golden path. The primary scenario is put inside of an if statement. Inside of the if statement, it could have more and more if else in some cases. Anyway, it will take time to find out the primary scenario which the method carries out.

func authenticateUser(_ username: String?, with password: String?) -> Bool {
guard let username = username, let password = password else {
//Bad input, do something and return
return false
//Golden path, everything goes well so far
let found = lookupDatabase(username, password)
return found

In the second piece of code, the golden path is clearly seen with guard keyword.

Last word

I believe that this is the purpose of guard, otherwise “if” is enough.

In term of clean code and coding convention, the golden path is recommended. Writing the code which follows this rule, is not always straightforward, but We should do it for many reasons.





Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s