Scala

Scala

Made by DeepSource

Use .lastOption to access the last element SC-P1001

Performance
Major

Certain Scala structures such as List implement methods such as .reverse that allows you to reverse the contents of a structure. This however can be an expensive operation and depends upon the kind of structure and the number of elements in it. Therefore, directly accessing the last element via .lastOption is more concise and performant than reversing an entire collection and then accessing the first element.

Bad practice

val lastElement = elements.reverse.headOption

Recommended

val lastElement = elements.lastOption