get followed by getOrElse instead of the two-argument getOrElse
ID |
scala.performance.get_get_or_else |
Severity |
info |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Inefficient Api Usage |
Language |
Scala |
Description
Looking a key up with get allocates an Option only to unwrap it on the next call. Map offers getOrElse(key, default), which returns the default directly and reads more clearly.
Rationale
Looking a key up with get allocates an Option only to unwrap it on the next call. Map offers getOrElse(key, default), which returns the default directly and reads more clearly.
The following code illustrates the pattern detected by this rule:
class GetGetOrElse {
def lookup(config: Map[String, String], key: String): String = {
// FLAGGED: get followed by getOrElse instead of the two-argument getOrElse
config.get(key).getOrElse("default")
}
}