Scala

Scala

Made by DeepSource

Prefer using Option.isDefined, Option.isEmpty or Option.nonEmpty instead of Option.size SC-R1004

Anti-pattern
Minor

Scala's collections return either Some or None when retrieving elements depending on whether the requested element exists or not. Therefore, the idiomatic and right approach is to use Option.isDefined, Option.isEmpty or Option.nonEmpty instead of Option.size.

Bad practice

val details = Users.getDetails(emailId)

// Incorrect approach
if (details.size == 0) {
  // ...
}

Recommended

val details = Users.getDetails(emailId)

// Correct approach
// Alternately, you can use pattern-matching as well
if (details.isDefined) {
  // ...
}