SimplifyBooleanReturn
Since Checkstyle 3.0
Description
Checks for over-complicated boolean return or yield statements. For example
the following code
if (valid())
return false;
else
return true;
could be written as
return !valid();
The idea for this Check has been shamelessly stolen from the equivalent PMD rule.
Examples
To configure the check:
<module name="Checker">
<module name="TreeWalker">
<module name="SimplifyBooleanReturn"/>
</module>
</module>
Example:
class Example1 {
boolean cond;
int a,b;
boolean check1() {
if (cond) { // violation, 'Conditional logic can be removed
return true;
} else {
return false;
}
}
boolean check1Simplified() {
return cond;
}
boolean check2() {
if (cond == true) { // violation, 'Conditional logic can be removed'
return false;
} else {
return true;
}
}
// Ok, can be simplified but doesn't return a Boolean
int choose1() {
if (cond) {
return a;
} else {
return b;
}
}
int choose1Simplified() {
return cond ? a: b;
}
}
Example of Usage
Violation Messages
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
Package
com.puppycrawl.tools.checkstyle.checks.coding