Scala

Scala

Made by DeepSource

Range's upper bound is equal to a length-like property SC-W1084

Bug risk
Major

Ranges in Scala can be declared via using the to and until methods. The to method is inclusive of the upper bound whereas the until method is exclusive. It is therefore recommended that you use the until method when counting upto a length-like property. By doing so, you don't have to worry about handling the off-by-one errors yourself in most cases.

Bad Practice

for (i <- 0 to container.length) {
  // ...
  //
  // The following check becomes redundant when `until` is used.
  if (i == container.length) {
    // Break?
  }
}

Recommended

if (i <- 0 until container.length) {
  // ...
}