Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

97
Views
Cleaner way of comparing class member variables

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!