Using the 'when' Expression for Cleaner Conditional Logic

The 'when' expression in Kotlin provides a more readable and concise way to handle multiple conditional branches compared to traditional if-else statements. It can be used as an expression, meaning it can return a value, which makes it ideal for assigning values based on conditions. Additionally, it can match against types, ranges, and even conditions directly, enhancing code clarity.


fun getColorDescription(color: String): String {
    return when (color) {
        "RED" -> "Color of passion and energy"
        "GREEN" -> "Color of nature and tranquility"
        "BLUE" -> "Color of calm and stability"
        "YELLOW" -> "Color of happiness and optimism"
        else -> "Unknown color"
    }
}

// Usage
val colorDescription = getColorDescription("RED") // Output: Color of passion and energy