Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite / check our equals/hashcode methods #1504

Open
4 tasks
sbernard31 opened this issue Sep 1, 2023 · 0 comments
Open
4 tasks

Rewrite / check our equals/hashcode methods #1504

sbernard31 opened this issue Sep 1, 2023 · 0 comments
Labels
housekeeping Refactoring, cleaning code or API

Comments

@sbernard31
Copy link
Contributor

Investigating on NPE in LwM2mPath.hashcode() find at #1502.

I decide to add unit tests to check the bug before to fix it.

I found that library to do that job : EqualsVerifier.

Good news : it does a very good job.
Bad news: this is more complicate than expected to implement good custom equals/hashcode method.

We should :

  1. Have a look at all custom equals/hashcode methods and add a corresponding test based on EqualsVerifier.
  2. Decide some convention about written equals/hashcode in Leshan.
  3. Reimplement equals/hashcode if needed.
  4. Add documentation about that in : https://github.com/eclipse-leshan/leshan/wiki/Code-&-design-guidelines#code-rules

Let's use this issue to talk about "Decide some convention about written equals/hashcode in Leshan".
If you interested about that you should first read : How to Write an Equality Method in Java

My current opinion :

  • we should probably use Objects.hash and Objects.hash

  • Use of this 3 a valid behavior :
    1. Use classic getClass() != obj.getClass() + making the class final : this is the simple way but I do not like too much the idea I prefer to let user being able to extend class.
    2. Use an instanceof + make hashcode/equals final : this is simple enough and this allow to extend the class but not to customize equals behavior...
    3. Use the canEqual method, which is a bit unusual but sounds to solve all problems.

    Should we encourage to use 3. in all code base ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
housekeeping Refactoring, cleaning code or API
Projects
None yet
Development

No branches or pull requests

1 participant