Scala

Scala

Made by DeepSource

Use pattern matching instead of repeated calls to isInstanceOf SC-R1029

Anti-pattern
Major

.isInstanceOf[T] allows you to check if an entity is of type T or not and is handy when used correctly. There is a simpler and more idiomatic way to write such checks though - using pattern matching. The benefits of pattern matching aside; note that any approach that relies extensively on Any, .isInstanceOf and .asInstanceOf defeats the language's type system and should be avoided.

Bad practice

if (t.isInstanceOf[Int]) {
  //
} else if (t.isInstanceOf[Double]) {
  //
}

Recommended

t match {
  case _: Int    =>
  case _: Double =>
}