Guard and If, Dilemma in Swift

Galih Samudra
Style Theory Engineering & Data
3 min readJun 10, 2020
Just a sky image for those of you who missed sky

It’s been known for a long-long time ago that “If” is an important conditional operators in many programming language.
Conditional operator are used to evaluate a condition that applied to one or two boolean expressions, and the result of the evaluation is either true or false. It is really important as without it programmers cannot code the logic that is designed for their designated purposes.
Here is a little example imagine that you want to make a simple function to validate:

Test function showing if implementation in Swift 5

Here on the code above you see that conditional operator “if” play an important part in determining the logic in the function, now what about “guard”?

In the swift programming language there is something called “guard”, a statement that can be used as conditional operator, here’s an example of using guard in the same logic as the function above.

Test function showing guard implementation as conditional operator in Swift 5

Now after you see in the code above that “guard” can also be used as conditional parameter, here’s the question why there is “guard” and “if” when both of them has the same function as conditional parameter?

Let’s look into this one by one shall we?

Here is the thing, there is one thing that “if” can do but, “guard” cannot, and what is that? It’s a second conditional statement or we could call it “else if”. In this “else if” you can use it to validate another conditional statement after the first condition got validated.

Test function showing else if implementation in Swift 5

After explaining the difference between “guard” and “if” there is actually a common functionality that is quite often being used by both, which is as an optional binding. What is optional binding well to explain that let me show you the example of optional binding using “guard” and “if”.

Test function showing optional binding using if and guard implementation in Swift 5

In the code above you can see that you used optional binding as a way to make sure that optional variable that may contain nil value does not have a nil value, this way we can make sure of the value of the variable and limited the chances of crash or bugs because of the nil value.

Now after reading all the explanation above, here is a little tips on which you should use between “guard” and “if” as conditional operators on your code.

  • You should use “guard” as an optional binding solution on your code, why? because it help on reducing unrelated logic to your code so it can be easier to read and understood
  • You should use “if” as conditional parameter on your code, why? because it can be easily adjustable depending on the logic that you want

Here is an example using “guard” and “if” according to the explanation above

Test function showing implementation using if and guard in Swift 5

Now you see in the code above it use “guard” as an optional binding to reduce the logic on the “if” conditional parameter to make the code easier to read and simplify the logic. It helps on making the code readable, traceable and maintainable.

Thanks for Reading

Hope this article brings you benefits in any kind of way, and thank you for reading this ;-)

— Reference

https://www.thoughtco.com/conditional-operator-2034056

--

--