OPT.UEQ
Avoid unnecessary equality operations with booleans
Description
This rule flags any unnecessary equality operation with booleans.
Comparing a boolean value with true is an identity operation (returns the same boolean value). The advantages of removing this unnecessary comparison with true are:
- The code will perform faster (the code generated has about 5 bytecodes less).
- The code becomes clearer.
Example
package OPT;
public class UEQ
{
boolean method (String string) {
return string.endsWith ("a") == true; // Violation
}
}
Repair
class UEQ_fixed
{
boolean method (String string) {
return string.endsWith ("a");
}
}
|