So, we’re now going to focus on early exits and what this is.
The concept of early exits is highly encouraged by Swift through those guard statements. They’re not very common in most programming languages and if they exist, the are not commonly used, but Swift encourages us to use them.
Before we dive not the early exits however, lets just take a look at what makes up a programming language. Most object oriented programming languages feature: functions, recursion and/or loops. What those concepts have in common, is that you execute a certain set of commands multiple times witch switching parameters.
Before those loops we usually have to set up a couple of things. There’s a little bit of setup work to do and in more complex cases we can even have loops with different sections that depend on different conditions.
Now to make our code more efficient we usually should check if all the conditions are met for our loop, before we start executing any work. If we do our setup work and enter the loop and then realize that our data is not ready and we can not do anything, we have wasted some precious time and energy before we got into the work. That’s work and energy that is wasted and to have your code efficient, you’d want to avoid that in general.
This is where we plug our early exits into the equation. An early exit could for example look something like this.
Demo in Xcode
Now that we’ve seen early exits in actual code let’s just get into what early exits really could say. We have 3 types of early exits. The first of them is the „return“ exit. Then we have the „break“ and finally „continue“.
No we’re going to dive into each of those and I’ll explain what they are and what they do.
Let’s focus on the „return“ statement, which looks a bit like this.
What this does is, abort the whole function that you’re inside and can send a value back to the caller. Thats the type of exit you’d want to use inside a function when you’re done with your work and return to what you were doing.
2. break lable name
The second is the break statement. Now the break statement looks something like this and is best used inside of switch-cases, if-statements or loops to break out of the scope completely.
In our example here this would mean that we would count from 0 to 4 because as our index hits five, it will jump into the if statement and the call of the break statement would cause us to leave the whole loop.
The last one is the „continue“. A continue looks something like this and can be used inside of loops. This is very useful if we just want to skip a certain element in our loop. For example in this loop, we count from 0 to 15 using our index and if our index equals 0 we will just continue and not count the 0.
In other words, if you would insert a print below the if statement it would print out 1 to 15 because as the 0 counts our continue would fire and skip the code that is beneath the continue and continue running with the next element of the loop, which is 1.
We could also test for 5 and that would cause only 0 to 4 being printed and then 6 to 15.
End of excursus
So now that we’ve got this down, this was just a quick side note to help you get your use of guards and if statements on track. You can now choose the right exit type for your specific problem.
For now I recommend that you hit pause in this class and jump into a Swift Playground and play around for a little while with those early exits to get a feeling for what they do, because you can’t use them everywhere. After that just come back and the next video will wait for you to tackle it.