Best Practices

radex.io has an excellent explanation of when you should AND shouldn’t use ‘guard’ in Swift. 🙌

‘Guard’ has quickly become one of my favourite way to help simplify my code by getting rid of lots of pyramids of doom! If if if 🙅.

Example:

guard !array.isEmpty else { return }

replaces the need to do an if statement around the entire code block that relies on the array not being empty and just returns from the function as soon as it finds an empty array without executing any further code in the function! 🎉

Make sure to check it out and try guard out in your code!

http://radex.io/swift/guard/

Link