Affects PMD Version: 6.21.0
Rule: SuspiciousEqualsMethodName
Description:
I like to write code as the sample below shows. The equals(Object)
method checks the class of other
and calls equals(MyClass)
if applicable. This provides a few possible performance optimizations. First, if javac
knows that the parameter is of type MyClass
, then it will call equals(MyClass)
directly and thereby skip the performance impact of instanceof
operator. Second, if JIT knows that the parameter is always of type MyClass
, then it will write the executable code to call equals(MyClass)
directly.
The problem is that SuspiciousEqualsMethodName
flags the code and could cause programmers to miss the potential optimizations. On the other hand, classes that derive from MyClass
have to override both equals()
and thus having two equals()
is error prone. (Perhaps a PMD rule could catch this.)
So, I post this as something to consider knowing it may not be a good idea to change SuspiciousEqualsMethodName
.
Code Sample demonstrating the issue:
public class MyClass { public boolean equals(Object other) { if (other instanceof MyClass) return(equals((MyClass) other); return(false); } public boolean equals(MyClass other) { ... } }
Running PMD through: Eclipse
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4