Swift — guard and why?

Swift — guard and why?

So i’ve been asked multiple times why and when should I use the?guard?statement?The?guard?statement was first introduced in Swift 2.0.

guard?combines two important concepts in the Swift language, optional wrapping (if you’re not sure what optional wrapping is, I suggest you check my article regarding “Optional Wrapping”?here) and where clause.

So, how can guard help us with optional unwrapping?

Assuming we have a?User?object which contains multiple fields and we need them all to use that object, let’s watch the following code snippet:

No alt text provided for this image

We can see?in the code snippet above the horrible thing called?Pyramid of Doom?which is something we dont like to do/see as a developer.


So how can we avoid this horrible?Pyramid of Doom?and what is the entire topic for this article,?guard?, of course, the statement that was given to us back in Swift 2.0 comes to our rescue, so how do we use this?guard?statement?

Well, there are multiple ways we can do, i’ll present some of them here, let’s go back again to our?User?object which contains multiple fields , and we want to use all those fields:

No alt text provided for this image

So as we can see in the code snippet above, the horrible pyramid is gone, instead we have multiple clauses with each unwrapping a field of?User?, if the condition is not met and the value cannot be unwrapped we fall into the else clause, in the else clause we print into our console the reason for failing.

Another approach will be to gather all the conditions together, example:

No alt text provided for this image

In the snippet above we use one?guard?statement to unwrap all fields together, this is not recommended, if we need to know or have a fallback for each of the fields.

guard vs If statement

It’s important to mention what we can achieve with?guard?we can also achieve with an?if?statement,?guard?makes it easier to read and prevents nesting conditions, example:

No alt text provided for this image

In the snippet above we can see a simple?if?condition, the condition checks if the?user.name?field equals John, we can achieve the same thing with the?guard?statement, example:

No alt text provided for this image

The snippet above shows an example of?guard, we can see that the?guard?statement forces us to?return?if the condition is not met (in this case if the?user.name?does not equal John), otherwise we can continue because the condition is met.

Important?to mention, that unlike?if?,?guard?wont continue the current scope if the condition is not met, and will?return?inside the?else?clause.

Conclusion

The use of guard statement can make our code more readable and maintainable,?guard?is not always the best solution but more situation dependent.?guard?can help you unwrap needed values that the current scope cannot run without,?guard?has it pros, it improves the code clarity and removes the need for nesting conditions .

for further reading click?here.

要查看或添加评论,请登录

Adi Mizrahi的更多文章

  • Swift 6.0 Actors: Eliminating Race Conditions!

    Swift 6.0 Actors: Eliminating Race Conditions!

    Hey Swift devs! If you're anything like me, you're always on the lookout for ways to make your code cleaner, safer, and…

  • iOS vs. Android: How to Decide?

    iOS vs. Android: How to Decide?

    Introduction In the dynamic landscape of mobile app development, choosing the right platform is a critical decision…

    4 条评论
  • iOS: Downsampling for Improved Performance

    iOS: Downsampling for Improved Performance

    Introduction When developing iOS applications, managing images efficiently is crucial for delivering a smooth user…

    3 条评论
  • Optimizing Mobile Apps for Extended Battery Life: Tips for Android and iOS Developers

    Optimizing Mobile Apps for Extended Battery Life: Tips for Android and iOS Developers

    Introduction Mobile devices have become an integral part of our daily lives, but their limited battery life remains a…

    3 条评论
  • WWDC Highlights for Developers

    WWDC Highlights for Developers

    WWDC 2023 took place on June 5th at Apple Park, featuring several significant announcements for developers. Here are…

  • Mobile Image Optimization

    Mobile Image Optimization

    Motivation As mobile developers, we want to build successful applications, rich applications, and visual applications…

  • Swift — Optional

    Swift — Optional

    Swift language introduced us to a new concept called Optional. If you’re new to Swift the concept might seem unfamiliar…

社区洞察

其他会员也浏览了