if
statement where possible SC-R1082Instead of assigning values in the then and else clauses of an if
statement, consider using the if
statement itself as an expression.
This allows you to directly assign the returned value to a val
. This syntax is clear and simpler.
// Requires that `i` be declared as `var` for now.
var i = 0
if (condition) {
i = 1
} else {
i = 2
}
// `i` can be a `val` now.
val i = if (condition) {
1
} else {
2
}