Scala

Scala

Made by DeepSource

Consider returning values from if statement where possible SC-R1082

Anti-pattern
Major

Instead 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.

Bad Practice

// Requires that `i` be declared as `var` for now.
var i = 0
if (condition) {
  i = 1
} else {
  i = 2
}

Recommended

// `i` can be a `val` now.
val i = if (condition) {
  1
} else {
  2
}