53 lines
1.2 KiB
Plaintext
53 lines
1.2 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)
|
|
|
|
include::message.adoc[]
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
include::comments-and-links.adoc[]
|
|
endif::env-github,rspecator-view[]
|