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:
public class Test { private boolean cond; private Foo a; private Foo b; public boolean check1() { if (cond) { // violation, can be simplified return true; } else { return false; } } // Ok, simplified version of check1() public boolean check2() { return cond; } // violations, can be simplified public boolean check3() { if (cond == true) { // can be simplified to "if (cond)" return false; } else { return true; // can be simplified to "return !cond" } } // Ok, can be simplified but doesn't return a Boolean public Foo choose1() { if (cond) { return a; } else { return b; } } // Ok, simplified version of choose1() public Foo choose2() { 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