resources.report.rules.pmd.SimplifiedTernary.html Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sanity4j Show documentation
Show all versions of sanity4j Show documentation
Sanity4J was created to simplify running multiple static code
analysis tools on the Java projects. It provides a single entry
point to run all the selected tools and produce a consolidated
report, which presents all findings in an easily accessible
manner.
The newest version!
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();
}
}