# Clean Up Your Conditional Logic: Exploring Kotlin 2.5.0-Beta2's New Boolean & Nullable Utilities

DevFeed: [Clean Up Your Conditional Logic: Exploring Kotlin 2.5.0-Beta2's New Boolean & Nullable Utilities](<https://devfeed.tech/articles/clean-up-your-conditional-logic-exploring-kotlin-2-5-0-beta2-s-new-boolean-nullable-utilities-122052.md>)

Original publisher: [Read original article](<https://proandroiddev.com/clean-up-your-conditional-logic-exploring-kotlin-2-5-0-beta2s-new-boolean-nullable-utilities-7854207156ea?source=rss-711ab22c5c77------2>)

Author: Nav Singh

Published: 2026-11-07T02:22:10Z

Content type: tutorial

Language: en

Sources: [Nav Singh](<https://devfeed.tech/sources/stories-by-nav-singh-on-medium.md>)

Topics: [Kotlin](<https://devfeed.tech/topics/kotlin.md>), [conditionals](<https://devfeed.tech/topics/conditionals.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>), [if statement](<https://devfeed.tech/topics/if-statement.md>), [Gradle](<https://devfeed.tech/topics/gradle.md>), [scope function](<https://devfeed.tech/topics/scope-function.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [android-app-development](<https://devfeed.tech/tags/android-app-development.md>), [androiddev](<https://devfeed.tech/tags/androiddev.md>), [boolean](<https://devfeed.tech/tags/boolean.md>), [clean-code](<https://devfeed.tech/tags/clean-code.md>), [conditional](<https://devfeed.tech/tags/conditional.md>), [kotlin](<https://devfeed.tech/tags/kotlin.md>), [kotlin-2](<https://devfeed.tech/tags/kotlin-2.md>), [standard-library](<https://devfeed.tech/tags/standard-library.md>)

## AI overview

Kotlin 2.5.0-Beta2 introduces three experimental standard-library utilities for conditional logic. `Boolean.onTrue` and `onFalse` run a supplied block when the Boolean matches, then return the original Boolean unchanged; the excerpt shows them used to trigger authentication messages while preserving the verification result. `ifOrNull(condition) { ... }` evaluates its block only when the condition is true, returning its value or `null`, as in the phone-number example. Use requires opting in with `ExperimentalStdlibApi`, either at a file or function or through the shown Gradle source-set setting. The supplied excerpt does not describe further behavior or limitations.

## Source excerpt

AI generated image Ditch redundant if-else boilerplate and awkward scope chaining with the newly introduced onTrue(), onFalse(), and ifOrNull() functions. Kotlin has always excelled at eliminating boilerplate. From concise data classes and standard scope functions (apply, also, let, takeIf) to smart casting, the language consistently seeks ways to make everyday idioms more expressive. Kotlin 2.5.0-Beta2 adds standard library functions that let us inspect a value before returning it, or return a nullable result based on that value. https://kotlinlang.org/docs/whatsnew-eap.html?utm_campaign=kotlin-releases&utm_content=2-5-0-beta2 Boolean.onTrue(action: () -> Unit): Boolean Boolean.onFalse(action: () -> Unit): Boolean ifOrNull(condition: Boolean, action: () -> T): T? Implementation Because these functions are introduced as experimental APIs in the standard library, we need to opt in to use them. File/Function Annotation OptIn(ExperimentalStdlibApi::class) Module-wide via Gradle config kotlin { sourceSets.all { languageSettings.optIn("kotlin.ExperimentalStdlibApi") } }onTrue() & onFalse() Both functions invoke a supplied block when the receiver matches the condition and return the original boolean unmodified. Example data class User(val id: String, val email: String) class AuthService() { // Before: fun authenticateLegacy(user: User, token: String): Boolean { val isValid = verifyToken(user.email, token) if (isValid) { "Auth.success" } else { "Auth.failure" } return isValid } // Now: @OptIn(ExperimentalStdlibApi::class) fun authenticate(user: User, token: String): Boolean = verifyToken(user.email, token) .onTrue { "Auth.success" } .onFalse { "Auth.failure" } }ifOrNull() When shaping API responses or DTOs, we may return one computed value if a flag is true and another if false. Traditionally, this looks like 👇 val phoneNumber = if (user.hasContactInfo) user.phoneNumber else nullThe New Way: ifOrNull() ifOrNull(condition) { //.. } lazily evaluates the lambda block only if