Option.isDefined
, Option.isEmpty
or Option.nonEmpty
instead of Option.size
SC-R1004Scala'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
.
val details = Users.getDetails(emailId)
// Incorrect approach
if (details.size == 0) {
// ...
}
val details = Users.getDetails(emailId)
// Correct approach
// Alternately, you can use pattern-matching as well
if (details.isDefined) {
// ...
}