In Kotlin, if
and when
statements are expressions that return a value. This allows for a more concise and functional
programming style with less cognitive complexity, because it results in fewer return points and fewer variable assignments in a function.
If both branches of an if
statement end with a return
statement, the if
statement should be used instead as
an expression for a return
statement.
If all branches of an exhaustive when
statement end with a return
statement, the when
statement should be
used instead as an expression for a return
statement. A when
statement is exhaustive when it covers all elements of an enum
or features an else
clause.
What is the potential impact?
Readability and Understanding
This change makes it easier to understand a function because it will reduce its complexity. This is because the function now contains fewer return
points that the developer needs to keep track of.