rspec/rules/S1221/java/rule.adoc
Fred Tingaud 16f6c0aecf
Inline adoc when include has no additional value (#1940)
Inline adoc files when they are included exactly once.

Also fix language tags because this inlining gives us better information
on what language the code is written in.
2023-05-25 14:18:12 +02:00

74 lines
2.6 KiB
Plaintext

== Why is this an issue?
Naming a method ``++tostring++``, ``++hashcode++`` or ``++equal++`` is either:
* A bug in the form of a typo. Overriding ``++toString++``, ``++Object.hashCode()++`` (note the camelCasing) or ``++Object.equals++`` (note the 's' on the end) was meant, and the application does not behave as expected.
* Done on purpose. The name however will confuse every other developer, who may not notice the naming difference, or who will think it is a bug.
In both cases, the method should be renamed.
=== Noncompliant code example
[source,java]
----
public int hashcode() { /* ... */ } // Noncompliant
public String tostring() { /* ... */ } // Noncompliant
public boolean equal(Object obj) { /* ... */ } // Noncompliant
----
=== Compliant solution
[source,java]
----
@Override
public int hashCode() { /* ... */ }
@Override
public String toString() { /* ... */ }
@Override
public boolean equals(Object obj) { /* ... */ }
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Either override "[Object.hashCode()|Object.equals]", or totally rename the method to prevent any confusion.
'''
== Comments And Links
(visible only on this page)
=== relates to: S1201
=== is related to: S1845
=== on 11 Feb 2015, 18:15:50 Nicolas Peru wrote:
@Ann I linked this issue to RSPEC-1201, I'll let you update description and findbugs reference.
=== on 11 Feb 2015, 20:25:04 Ann Campbell wrote:
\[~nicolas.peru] are you saying you'd like to roll the content of this rule into RSPEC-1201? Because they're actually about (slightly) different things...
=== on 12 Feb 2015, 07:18:50 Nicolas Peru wrote:
\[~ann.campbell.2] I am saying that this rule should not cover the equals method as it is covered by RSPEC-1201. I don't see the difference between the two rules except that one concern equals and the other one hashcode.
=== on 12 Feb 2015, 13:53:05 Ann Campbell wrote:
\[~nicolas.peru], this rule doesn't cover "equals" (with an 's'), it covers "equal". I.e. this rule is about method names that are not-quite-right. RSPEC-1201 is about signatures that are not-quite-right. Or are you saying that in the implementation of RSPEC-1201 you also check this spelling variation?
=== on 12 Feb 2015, 14:23:05 Nicolas Peru wrote:
\[~ann.campbell.2]My bad, I really did not spot the difference between the two rules... I tend to think this one should clearly mention that it detects typo. So this mean that not only description but implementation is out of date in plugin. ticket created : \http://jira.codehaus.org/browse/SONARJAVA-902
endif::env-github,rspecator-view[]