Scala style warning

Hi,

I have the following warning twice with Scalastyle:
Eliminate redundant if expressions where both branches return constant booleans
The faulty code is:
if (monFeu == Rouge) true
else if (monFeu == Orange) {
if (leurFeu == Orange || leurFeu == Vert) // <- Here
true
else false
} else if (leurFeu == Orange || leurFeu == Vert) // <- here
true
else false
}

How should I fix it ? Code works as intended.

Best regards,

A.B.

As a general rule, any if that produces the values true and false like this should be simplified to just the conditional expression. If if (leurFeu == Orange || leurFeu == Vert) true else false should be simplified to just (leurFeu == Orange || leurFeu == Vert).

Hello,

if(expr) true else false

can be simply replaced by

expr
.

So:

if (monFeu == Rouge) true else if (monFeu == Orange) { (leurFeu ==
Orange || leurFeu == Vert) } else { (leurFeu == Orange || leurFeu == Vert)
}

This further simplifies to:

*if (monFeu == Rouge) true else { (leurFeu == Orange || leurFeu == Vert)
} *

which further simplifies to

(monFeu == Rouge) || (leurFeu == Orange || leurFeu == Vert)

Best, Oliver

MarkCLewis http://users.scala-lang.org/u/markclewis
February 14

As a general rule, any if that produces the values true and false like
this should be simplified to just the conditional expression. If |if
(leurFeu == Orange || leurFeu == Vert) true else false| should be
simplified to just |(leurFeu == Orange || leurFeu == Vert)|.

OK, I understand.
Thanks for your answer.

Alain