I have code like that:
optionBoolean.getOrElse(false) && otherOptionBoolean.getOrElse(false)
And Scalastyle tells me that it can be simplified. How?
You can try the following:
Seq(optionBoolean, otherOptionBoolean).forall(_.contains(true))
In Scala 2.13 (it is very similar in prior versions) the forall
method is located at IterableOnce
, and its implementation is:
def forall(p: A => Boolean): Boolean = {
var res = true
val it = iterator
while (res && it.hasNext) res = p(it.next())
res
}
Therefore once there is a value that doesn't satisfy the condition, the loop will break, and the rest will not be tested.
Code run at Scastie.