Why Object.hashCode() doesn't follow Java code conventions
Is there any specific reason why hashCode is the only public method of the Object class which doesn't follow Sun's suggested Java code conventions and later Oracle's Java code conventions? I mean can they name it toHashCode() or getHashCode() or createHashCode()?
Edit: I'm talking about code conventions for the Java programming language (oracle.com/technetwork/java/codeconvtoc-136057.html). These conventions are referenced in Oracle's "OCA Java SE 7 Programmer I Study Guide (Exam 1Z0-803) (Oracle Press) - Liguori, Robert".
In the documentation, we can see something like this: "Methods should be verbs with mixed initials and lowercase, and capitalize the first letter of each internal word."
AFAIK hashcode is not a verb.
I think it does follow convention. It all depends on the convention you're talking about .
The OP is particularly interested in the code conventions of the Java programming language , and Chapter 9 covers the conventions for method names . First of all, it should be noted that the documentation is no longer maintained and contains numerous caveats:
...the information itself may no longer be valid. This document was last revised on April 20, 1999
Of course, the hashCode()
method predates 1999, so we can refer to the documentation to see if the method in question violates the convention at the time. The document states:
Methods should be verbs with mixed initials and lowercase, and capitalize the first letter of each internal word.
Regarding hashCode()
, there is no dispute that the initial lowercase is mixed case . However, the OP seems to think it violates the convention that methods should be verbs ; the implicit assumption is that "hashcode" or "hashcode" is a noun.
Before making a judgment, let's look at the third part of the convention: the first letter of every internal word is capitalized . If you briefly assume that the original author followed these conventions, capitalized hashCode()
means that their author treats "hash" and "code" as separate words. If you treat them separately, the word "hash" is a verb in English . With this interpretation, all parts of the convention can be satisfied.
It's accepted that the term "hash code" has become (at least) a common term among Java developers, and is often treated as a noun - probably largely due to the name of the method and very little. (chicken v. egg?), but only the original author can speak for himself .
In my original answer, I used JavaBeans conventions as an example:
Beans are Java classes whose method names follow the JavaBeans guidelines. The Bean Builder tool uses introspection to inspect bean classes. Based on this inspection, the Bean Builder tool can determine the properties, methods, and events of the bean.
In JavaBeans, properties are accessed through "getter" methods, i.e., by calling to read the "foo" property getFoo()
. However, the hashCode()
method is in the Java languageA technical requirement for all subclasses , Object
but generally not a property of the "business logic" represented by the object. If you write a class to represent fruit, then that feature getColor()
, isSkinEdible()
etc. If it weren't for the technical requirements of Java , you probably wouldn't have considered the method of writing etc getHashCode()
, because...have you ever found a living banana hash code?
If it hashCode()
is named getHashCode()
then, according to that convention, JavaBeans must treat it specially to ignore it. Otherwise, it will always check that "property" for things that are usually rarely used in the main logic of the program.
I can't cover all possible conventions in this answer, but I have these thoughts on the other examples given in the question:
createHashCode()
- Even by convention, I wouldn't use it becausehashCode()
one is returnedint
(primitives) and they are not created like reference types (objects). I think this is the wrong verb.toHashCode()
- To me, this represents a transformation. But that's not whathashCode()
. I don't have any representation expectations if itfoo.hashCode()
returns . it is based on42
42
foo
foo
Instance information is calculated , but has no other actual correlation. Many other instances (many classes) may be returned, so are not a substitute or analog for42
any instance .