Skip to content
DeveloperMemos

Understanding "Assignment can be lifted out of when" in Kotlin

Kotlin, Programming1 min read

Before diving into the intricacies of "Assignment can be lifted out of when," let's first understand the basics of the when expression in Kotlin. Similar to the switch statement in other languages, when allows you to express conditions in a concise and readable manner.

Here's a simple example:

1fun describeNumber(number: Int): String {
2 return when (number) {
3 0 -> "Zero"
4 1 -> "One"
5 2 -> "Two"
6 else -> "Other"
7 }
8}

In this example, the when expression evaluates the value of number and returns a corresponding string. The else branch acts as a catch-all for any value not covered by the previous cases.

"Assignment can be lifted out of when"

The phrase "Assignment can be lifted out of when" refers to a Kotlin compiler warning that suggests moving an assignment statement outside of the when expression when the same value is being assigned in multiple branches. This can lead to more concise and readable code.

Let's consider an example to illustrate this concept:

1fun getColorName(color: Int): String {
2 val name: String
3 when (color) {
4 1 -> name = "Red"
5 2 -> name = "Green"
6 3 -> name = "Blue"
7 else -> name = "Unknown"
8 }
9 return name
10}

In this function, the variable name is assigned different values based on the value of color in each branch of the when expression. The compiler recognizes that the assignment is consistent across all branches and suggests lifting the assignment out of the when expression:

1fun getColorName(color: Int): String {
2 val name: String = when (color) {
3 1 -> "Red"
4 2 -> "Green"
5 3 -> "Blue"
6 else -> "Unknown"
7 }
8 return name
9}

This refactoring makes the code more concise and eliminates redundancy by assigning the value directly within the when expression.

Benefits of Lifting Assignment out of when

  1. Conciseness: By lifting the assignment out of the when expression, you eliminate unnecessary repetition of variable assignments, resulting in more concise code.

  2. Readability: The refactoring makes the code easier to read and understand, as it clearly expresses the intent of assigning a value based on a condition.

  3. Reduced Risk of Bugs: Redundant assignments can introduce the risk of bugs, especially if modifications are made to one branch but not to others. Lifting the assignment minimizes this risk.

Considerations and Limitations

While the "Assignment can be lifted out of when" warning is generally helpful, there are scenarios where it might not be applicable or necessary. For instance, if the assigned values are more complex computations or if the conditions involve multiple variables, it might be more appropriate to keep the assignment within the when expression. Additionally, the warning is only generated when the compiler can detect that the assignments are consistent across all branches. If the assignments involve different expressions or computations the warning will not be issued.