# Swift's Guard Statement for Kotlin

DevFeed: [Swift's Guard Statement for Kotlin](<https://devfeed.tech/articles/swift-s-guard-statement-for-kotlin-25890.md>)

Original publisher: [Read original article](<https://medium.com/swlh/swifts-guard-statement-for-kotlin-967ba580443f?source=rss-1331e67af4e1------2>)

Author: Danny Preussler

Published: 2020-12-07T21:55:37Z

Content type: tutorial

Language: en

Sources: [Stories by Danny Preussler on Medium](<https://devfeed.tech/sources/stories-by-danny-preussler-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [Swift](<https://devfeed.tech/topics/swift.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [code](<https://devfeed.tech/tags/code.md>), [condition](<https://devfeed.tech/tags/condition.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [programming](<https://devfeed.tech/tags/programming.md>), [swift](<https://devfeed.tech/tags/swift.md>)

## AI overview

This tutorial compares Swift's guard statement with Kotlin's early-return patterns. It explains how guard exits early when conditions fail, then demonstrates Kotlin alternatives using returns, expressions, exceptions, and a wrapper for handling arbitrary lambdas.

## Source excerpt

https://unsplash.com/photos/znMu0Enj_Gw Even if you live on the Kotlin side of things maybe once in a while you also check Swift code. If you do, you probably noticed how similar both languages are. To me, Kotlin looks more concise but I'm also biased as I work with Kotlin daily. On the other hand, Swift has some great features too. One is the guard keyword, a really nice tool Swift developers have that we are missing. Can we bring it to Kotlin? What is guard? guard is used to exit a block of code early, if a given condition is not met. This way all the code following the guarded variable can be safely executed. Let's look at an example where we do some calculation but only if the given input can be converted into an integer: func printResultFor(input: String) -> Void { guard let result = Int(input) else { println("input was not an integer") return } // function continues with valid int print("result: ", 100 * result)} Of course, simple code like this could be written with the standard if/else statements! But the power of guard becomes apparent when many of these statements are used in succession. Written in a traditional way this can easily lead to a hell of nested ifs where you make sure everything is safe in the innermost block when all the if are true. Take a simple sign up flow for example, where the customer needs to enter user, password, and their age for validation. We have to make sure those are not nil or empty and check for a valid age. func submit(usernameText: String?, passwordText: String?, ageText: String?) { guard let username = usernameText, !username.isEmpty else { print("username is not set or blank") return } guard let password = passwordText, !password.isEmpty else { print("password is not set or blank") return } guard let ageString = ageText else { print("age is not set") return } guard let age = Int(ageString), age > 18 else { print("age not valid") return } // all values are checked and valid here register(username, password, age)} Here the r