Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking objects for identity and Sonar issues

We are checking the quality of our code using Sonar, and Sonar found code which compares objects for identity like this:

if (cellOfInterest == currentCell) { … }

Sonar finds this kind of identity check peculiar enough to call it critical and proposes to replace the identity check (using ==) with a check for equality (using .equals() instead). Identity checks, so the rationale behind this, are often not what is meant.

In our case, however, we iterate through a list of Cells and check in each iteration (currentCell) whether we are handling a special cell we already have (cellOfInterest).

I'd like to hear if there are other patterns than ours which are common and which avoid this issue simply by using a different design. Or what solutions do you propose to avoid using an identity check in the mentioned situation?

We considered a replacement of the identity check with an equality check as described above but it does not seem applicable in our situation because other cells might be "equal" as well but not identical.

All ideas are welcome!

like image 379
Alfe Avatar asked Jun 19 '15 15:06

Alfe


1 Answers

If identity is what you need then it is what you need. The warning makes sense as it is often a bug but in this case it's not.

As an example, IdentityHashMap (which works with identity vs. equality) has this in its javadoc:

This class is not a general-purpose Map implementation! [...] This class is designed for use only in the rare cases wherein reference-equality semantics are required.

So it is rarely useful but has its uses. You are in a similar position. And of course, its internal code does exactly what you expect, it uses == to get a key.

Bottom line: I don't see why you would need to make the code more complex just because some static code analysis tool says it may be a problem - such a tool is supposed to help you, not to force you into some weird construct that will essentially do the same thing. I would explain why == is used in a comment for clarity, mark it as a false positive and move on.

If you really want to remove the warning, the only option I can think of is to use equals and change the equals method to either:

  • the default Object#equals which is based on identity
  • some implementation that uniquely identifies the cells, maybe based on some unique id or coordinates?
like image 185
assylias Avatar answered Sep 28 '22 14:09

assylias