Why Object.hashCode() doesn't follow Java code conventions


cookies

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.

William Price

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 , Objectbut 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 because hashCode()one is returned int(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 what hashCode(). I don't have any representation expectations if it foo.hashCode()returns . it is based on4242foofooInstance information is calculated , but has no other actual correlation. Many other instances (many classes) may be returned, so are not a substitute or analog for 42any instance .

Related


Why doesn't the pattern string follow this code?

Lenovo Genievia: The following code is executed: DecimalFormat eNotation1 = new DecimalFormat("#0.###E0"); System.out.println(eNotation1.format(123.456789)); my expected output; 1.235E2 replace, 1.2346E2 printing. Why output 1.2346E2? I know I can use a s

Why doesn't Python's logging module follow PEP8 conventions?

BorageX: This is just a curiosity for historical purposes: I'm wondering if anyone knows why the very widely used (and core module) logging doesn't follow Python's PEP-8 naming convention . For example, in >>> import logging >>> log = logging.getLogger("hello"

Why Android conventions don't follow regular Java

Necronomicon: So I've been working on android for about 2 years and Java for 6 years and from the beginning I liked where I found them at Java Convetion , a guide that helped me write more understandable code , you know that the upper layer of a typical Calmel

Why doesn't Array#keep_if follow Ruby conventions?

Sharvy Ahmed There is a Ruby convention for naming methods with bang( !) . The convention is selfthat we use bang to let others know about self-modifying behavior if the method changes. For example , wo Array#selectn't change self, but Array#select!will . But

Why doesn't the pattern string follow this code?

Lenovo Genievia: The following code is executed: DecimalFormat eNotation1 = new DecimalFormat("#0.###E0"); System.out.println(eNotation1.format(123.456789)); my expected output; 1.235E2 replace, 1.2346E2 printing. Why output 1.2346E2? I know I can use a s

Why doesn't Python's logging module follow PEP8 conventions?

BorageX: This is just a curiosity for historical purposes: I'm wondering if anyone knows why the very widely used (and core module) logging doesn't follow Python's PEP-8 naming convention . For example, in >>> import logging >>> log = logging.getLogger("hello"

Why doesn't Array#keep_if follow Ruby conventions?

Sharvy Ahmed There is a Ruby convention for naming methods with bang( !) . The convention is selfthat we use bang to let others know about self-modifying behavior if the method changes. For example , wo Array#selectn't change self, but Array#select!will . But

Why Object.hashCode() doesn't follow Java code conventions

cookies 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 create

Why doesn't Array#keep_if follow Ruby conventions?

Sharvy Ahmed There is a Ruby convention for naming methods with bang( !) . The convention is selfthat we use bang to let others know about self-modifying behavior if the method changes. For example , wo Array#selectn't change self, but Array#select!will . But

Why doesn't the implementation of Object.equals() use hashCode()?

Francois Bourgeois Or "Why did the Sun/Oracle guys force us to rewrite equals() and hashCode() every time?" Everyone knows that if you override equals() or hashCode() for one object, you must override the other object too, because there is a contract between t

Why Object.hashCode() doesn't follow Java code conventions

cookies Is there any specific reason why hashCode, the only public method of the Object class, 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 createHashCod

Why doesn't the hashcode generate a unique hashcode?

Geeks We know that the default implementation of hashCode() will return an integer after converting the internal address to the internal address of the object . Therefore, the internal memory of each object is different, which is why hashCode() does not genera

Why doesn't the pattern string follow this code?

Lenovo Genievia: The following code is executed: DecimalFormat eNotation1 = new DecimalFormat("#0.###E0"); System.out.println(eNotation1.format(123.456789)); my expected output; 1.235E2 replace, 1.2346E2 printing. Why output 1.2346E2? I know I can use a s

Why doesn't the pattern string follow this code?

Lenovo Genievia: The following code is executed: DecimalFormat eNotation1 = new DecimalFormat("#0.###E0"); System.out.println(eNotation1.format(123.456789)); my expected output; 1.235E2 replace, 1.2346E2 printing. Why output 1.2346E2? I know I can use a s

Why doesn't Python's logging module follow PEP8 conventions?

BorageX: This is just a curiosity for historical purposes: I'm wondering if anyone knows why the very widely used (and core module) logging doesn't follow Python's PEP-8 naming convention . For example, in >>> import logging >>> log = logging.getLogger("hello"

Why doesn't Array#keep_if follow Ruby conventions?

Sharvy Ahmed There is a Ruby convention for naming methods with bang( !) . The convention is selfthat we use bang to let others know about self-modifying behavior if the method changes. For example , wo Array#selectn't change self, but Array#select!will . But

Why Android conventions don't follow regular Java

Necronomicon: So I've been working on android for about 2 years and Java for 6 years and from the beginning I liked where I found them at Java Convetion , a guide that helped me write more understandable code , you know that the upper layer of a typical Calmel

Why doesn't the implementation of Object.equals() use hashCode()?

Francois Bourgeois Or "Why did the Sun/Oracle guys force us to rewrite equals() and hashCode() every time?" Everyone knows that if you override equals() or hashCode() for one object, you must override the other object too, because there is a contract between t

Why doesn't Kotlin follow Java syntax?

TareK Khoury I'm learning Kotlin as an Android developer! Out of curiosity, why don't the guys at JetBrains follow Java-style syntax (where possible) and make it easier for developers to learn Kotlin ? For example, define a simple function in Kotlin: fun simpl