Guard statement
A guard statement is basically a reverse if statement
|
|
Why do these even exist?
First of all, they make your code prettier. They can flatten some code structures like pyramids of validation. It takes less effort to read “guard” and know what it means instead of “if !() {}”. Secondly, it introduces a method of unwrapping an optional and placing it in the active scope, instead of inside a body like an “if let” statement.
Originally published 04/15/2021 @ https://pittcsc.org/ Discord
Published here on 09/08/2022. Blog published date reflects the original date of publication.