Since Checkstyle 3.0
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.
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; } }
All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.
com.puppycrawl.tools.checkstyle.checks.coding