Java rule S1067 ‘Expressions should not be too complex’ is a good rule, but there is one scenario where we find that we close issues raised by this rule as ‘Won’t Fix’ very frequently, and that is equals methods. It would be good if we had the option to tell the rule to ignore all equals methods.
For example, a typical equals method for a value object looks something like:
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final SomeDTO dto = (SomeDTO) o;
return a == dto.a &&
b == dto.b &&
getD().getC().equals(dto.getD().getC()) &&
e == dto.e &&
f.equals(dto.f);
}
The last return statement frequently breaks the S1067 condition but the high number of conditional operators used to do a field by field match in this circumstance is generally expected