Scala

Scala

Made by DeepSource

Pure methods should not have side-effects SC-R1058

Anti-pattern
Major

Parameterless methods in Scala are those methods that do not require any parentheses. Such methods are considered to be pure methods. A pure method is a method that does not have any side effects, such as mutating a state. Such methods always return the same value as long as the same set of arguments is passed to it. Since the this keyword is associated with an object's state, having a parameterless method use this violates the definition of a pure method. It is recommended that you either modify the method's logic to not rely on this or move away from the parameterless notation.

Bad Practice

// `something` is parameterless.
def something: Int = {
  // Do something
  this.val += increment
  this.val
}

Recommended

// Parentheses added to `something`.
def something(): Int = {
  // Do something
  this.val += increment
  this.val
}

References