The Guard Statement of Swift and the Optional relation

Giorgos Karakatsanis
2 min readJan 15, 2021
Photo by Kari Shea on Unsplash

Let’t do a quick dive into those statements and find the relations between them. Why do we use them, when and the restrictions trigger reactions…

The basic reason of creating optionals is to avoid crashing programs , if some variable doesn’t have a value.

Some values must be optionals like the real life. For example we don’t have all the people middle name or spouse… To handle such us this situation must use the above statements to avoid of using multiple pyramids of if…. then….else.

Of course from time to time we like using old methods like we use to, but like all things in computer science (and to the real world…), CHANGES!

“Everything Changes” Heraklitus Greek philosopher

People like their routines, even to programming! We need to be open mind to adapt all those new technics which coming with the programming languages progress.

To sum up Guard Statement maybe to the beginning it is hard to understand the logic of use.

On the first touch you say, “ok I got it… It is very simple and logical”, but when you try to add it in your code, you facing difficulties and you need to focus, to try some examples and then suddenly appear a tool that solve the most of nil problems and transform your code to readable and ease to understand!

--

--