# How-To use Guard in Swift

It took me a while to appreciate this one, but the Swift guard statement is very useful under the right circumstances. Let’s take a look at how we can use it to simplify code and maybe improve performance.

Think of guard as being a way to test something and avoid executing code, in some ways it is like the testing assert statement but let’s not worry about that now. The interesting thing is guard statements only run if the condition is not met rather than other logic operators that check until something is met and then run, take a look at this example (try it in a playground).

``````var myVar = 10

func checkNum(X:Int?) {
if (myVar < 10) {
print("myVar is less than 10")
} else if (myVar == 10) {
print("myVar is exactly 10")
} else {
print("myVar is more than 10")
}
}

checkNum(myVar) // Outputs "myVar is exactly 10\n"
``````

This example is a little simplistic but it is great for getting the idea across, we call the function sending a value and one of the three if statements are met and a line printed on the screen. Let’s say we only wanted to do something if the value is not 10, in this case, it is a simple print statement but there could be some crazy amount of functionality code going on that we want to avoid executing unless our condition is met for performance and easier code reading. Take a look at this version using the guard statement instead.

``````var myVar = 10

func checkNumGuard(X:Int?) {
guard let X = X where X != 10 else {
return
}
print ("all this code will be executed when X is not 10")
}

checkNumGuard(myVar) // Outputs nothing
``````

A lot less code right? What is going on here.

We call the function passing it myVar which has a value of 10. The guard statement says let x = x if the incoming x argument is not equal to 10. Otherwise return out of this function executing no more of the function code.

Change the value of myVar to anything other than 10 and the code after the guard statement will execute until the end of the function.

That is how guard works, it provides a great way to check something(s) and quit executing the function if those conditions are not met. This example is very simple and short, but imagine there is a lot more that the function would normally do that could consume CPU, memory or network bandwidth. All that can be avoided by performing the check and getting out of the function without further execution.

You can combine multiple statements using guard, let’s change the function to take two arguments. Our new scenario is that we only want the code in the function to execute if myVar has a value of 10 and our new myOtherVar has a value of 20. If either of these values are anything else we want code execution to stop and leave the function, each check is separated by a comma.

``````var myVar = 10
var myOtherVar = 20

func checkNumGuard(X:Int?, y:Int?) {
guard let X = X where X != 10, let y = y where y != 20 else {
return
}

print("myVar is not 10 and myOtherVar is not 20")
}

checkNumGuard(myVar, y: myOtherVar)
``````

Play around with the values in a storyboard and see what results you get. I think you will agree this is not only more readable but more efficient that a long complicated if or switch statement to check the conditions.

Hopefully this gets you thinking about ways you can use guard and how it also makes code more readable to developers.

1. I still don’t see the advantage over an if clause.

Couldn’t I just as easily write the same code this way?

var myVar = 10

func checkNumGuard(X:Int?) {

if let X = X where X == 10 {
return
}
print (“all this code will be executed when X is not 10”)

}

checkNumGuard(myVar) // Outputs nothing

2. > A lot less code right? What is going on here.

The functionality between checkNum and checkNumGuard is not equivalent. The simplified functionality of checkNumGuard in and of itself will reduce the amount of code.

Also, would you please expand upon how the following example

guard let X = X where X != 10 else {
return
}
print (“all this code will be executed when X is not 10”)

is simpler than:

if ( X != 10) {
print (“all this code will be executed when X is not 10”)
}

Or:

guard let X = X where X != 10, let y = y where y != 20 else {
return
}
print(“myVar is not 10 and myOtherVar is not 20”)

is simpler than:

if ( X != 10 && y != 20) {
print(“myVar is not 10 and myOtherVar is not 20”)
}

1. Hi Ray, thanks for the comments I appreciate them.

You are right, these examples are very simplistic to get the idea of guard across. I would expect that under real situations there would be a lot more code in a function than I have here. As you mention they are not saving a lot of code over the suggested ‘if’ blocks. I would offer that the guard is easier to read than nested if blocks, as I suggest in the post.

3. I refer you back to my reply to Ray. These examples are purposefully kept simple to help new users learn the idea and basic syntax behind the guard statement. I would expect it to be used in bigger blocks of code than those offered for the learning process here.
{"ft_lang_label":"__label__en","ft_lang_prob":0.914287,"math_prob":0.93699306,"size":4993,"snap":"2022-27-2022-33","text_gpt3_token_len":1193,"char_repetition_ratio":0.1365003,"word_repetition_ratio":0.14180672,"special_character_ratio":0.25075105,"punctuation_ratio":0.07884231,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9579976,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-07-04T05:39:16Z\",\"WARC-Record-ID\":\"<urn:uuid:50c95314-f209-45ef-a14c-b102f9e23b59>\",\"Content-Length\":\"62650\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ddc8e4ad-39c7-41bb-957c-4ee1b21ff626>\",\"WARC-Concurrent-To\":\"<urn:uuid:5cb9a251-ea9c-4224-9eb6-15af9d07df90>\",\"WARC-IP-Address\":\"45.40.148.117\",\"WARC-Target-URI\":\"https://grfxmedia.com/swift/basics/guard/\",\"WARC-Payload-Digest\":\"sha1:ZYA6FDYO2JGI75NUIAY5GRWRO2D7YDCF\",\"WARC-Block-Digest\":\"sha1:GE7H6OXXAF6H2REGOBFQAICZNZXYHBAD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656104354651.73_warc_CC-MAIN-20220704050055-20220704080055-00691.warc.gz\"}"}