
resources.report.rules.pmd.SimplifiedTernary.html Maven / Gradle / Ivy
SimplifiedTernary
SimplifiedTernary
Look for ternary operators with the form condition ? literalBoolean : foo or condition ? foo : literalBoolean.
These expressions can be simplified respectively to condition || foo when the literalBoolean is true !condition && foo when the literalBoolean is false or !condition || foo when the literalBoolean is true condition && foo when the literalBoolean is false
//ConditionalExpression[@Ternary='true'][not(PrimaryExpression/*/Literal) and (Expression/PrimaryExpression/*/Literal/BooleanLiteral)]
|
//ConditionalExpression[@Ternary='true'][not(Expression/PrimaryExpression/*/Literal) and (PrimaryExpression/*/Literal/BooleanLiteral)]
Example(s):
public class Foo {
public boolean test() {
return condition ? true : something(); // can be as simple as return condition || something();
}
public void test2() {
final boolean value = condition ? false : something(); // can be as simple as value = !condition && something();
}
public boolean test3() {
return condition ? something() : true; // can be as simple as return !condition || something();
}
public void test4() {
final boolean otherValue = condition ? something() : false; // can be as simple as condition && something();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy