Kotlin Tip #28: Use assert() to assert conditions in development— 100 Kotlin Tips in 100 Days

Raphael De Lio
Kotlin with Raphael De Lio
2 min readMar 8, 2024

Twitter | LinkedIn | YouTube | Instagram
Tip #27: Use require() for argument validation in functions or constructors

The assert() function in Kotlin evaluates a Boolean expression passed to it. If the expression evaluates to true, the program continues execution as normal. However, if the expression evaluates to false, an AssertionError is thrown, halting the execution. This mechanism is useful in development environments, where catching and rectifying assumptions that don’t hold is crucial.

The basic syntax of assert() is quite straightforward:

assert(condition) { "Assertion failed message" }

Here, condition is a Boolean expression. If condition is false, the AssertionError is thrown with the optional "Assertion failed message".

By asserting conditions, you can catch logical errors early in the development process. This is particularly useful during the debugging and testing phases, where identifying the root cause of an issue is crucial.

Consider an e-commerce application where you have a function to apply discounts to products. You might want to assert that the discount percentage is within a valid range (e.g., 0–50%).

fun applyDiscount(price: Double, discountPercentage: Int): Double {
assert(discountPercentage in 0..50) { "Discount percentage must be between 0 and 50" }
return price * (100 - discountPercentage) / 100
}

In this example, the assert() function ensures that the discountPercentage is within the expected range. If discountPercentage is outside this range, it indicates a logical error in the program, and an AssertionError is thrown.

However, ensure that assertions are disabled in your production builds to avoid unintended program termination in a user environment. To enable or disable, we can use the -ea (enable assertions) JVM argument. Moreover, assert() should not replace proper input validation or error handling for user input or external data sources, it is primarily a development tool.

By asserting critical assumptions, you can catch and rectify logic errors early, making your debugging process more efficient. Like many of Kotlin’s features, it is about giving developers the tools to express their intent clearly, ensuring that the code behaves as expected under all conditions.

I hope you have enjoyed this tip of our series! Don’t forget to subscribe and stay tuned for more Kotlin tips!

Stay curious!

Tip #29: Use Sequences to Boot Performance in Large Data Processing

Contribute

Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio

Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw

Follow Me on Social Media

Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.

Twitter | LinkedIn | YouTube | Instagram

--

--