I have a class with over 20 Strings and Ints. I am comparing the same value of each member with two instances. Not all Strings need to be compared but most. I'm using this for all Strings in one isEqual method:
return instance1.getCar().equalsIgnoreCase(instance2.getCar())
&& instance1.getHouse().equalsIgnoreCase(instance2.getHouse())
&& instance1.getPet().equalsIgnoreCase(instance2.getPet())...
Checkstyle is saying :
Cyclomatic Complexity is 17 (max allowed is 10). (67:5) [CyclomaticComplexity]
Is there a cleaner way to compare certain member variables of the same class in two instances? Thanks
You can go with lots of
return Objects.equals( instance1.getCar(), instance2.getCar() )
&& Objects.equals( instance1.getHouse(), instance2.getHouse() )
or alternatively use project Lombok, annotating your class with @EqualsAndHashCode and then put @EqualsAndHashCode.Exclude on fields you don't want to compare.