Scala

Scala

Made by DeepSource

Prefer using .get and then pattern matching over .contains for a Map SC-R1021

Anti-pattern
Minor

Scala's collections return either Some or None when retrieving elements depending on whether the requested element exists or not. It is therefore expected that when dealing with such collections, you use .get and then use pattern-matching over other approaches such as .contains.

Bad practice

if (map.contains(key)) {
  println("key exists")
}

Recommended

map.get(key) match {
  case Some(_) => println("key exists")
  case None    =>
}