# Code Smell 167 - Hashing Comparison

> TL;DR: If you check for the hash, you should also check for equality

# Problems

- [Bijection fault](https://maximilianocontieri.com/the-one-and-only-software-design-principle)

# Solutions

1. Check for hash (fast) and then check for Equality (slow)

# Context

On 2022 Oct 7th one of the larger blockchains had to be halted.

[This news](https://www.coindesk.com/business/2022/10/06/binance-linked-bnb-price-falls-close-to-4-on-hack-rumors/) was shocking since most blockchains are decentralized by definition.

You can read a full article here:

%[https://maximilianocontieri.com/how-a-hacker-stole-566m-usd-exploiting-a-code-smell]
	     
# Sample Code

## Wrong

[Gist Url]: # (https://gist.github.com/mcsee/b97afa5814c25e6d9c53e35f3fc5f09e)
```java
public class Person {
 
public String name;
// Public attributes are another smell  
 
 @Override
 public boolean equals(Person anotherPerson) {
   return name.equals(anotherPerson.name); 
 }
 	
@Override
 public int hashCode() {
   return (int)(Math.random()*256); 
 }
 // This is just an example of non-correlation  
 
 // When using HashMaps we can make a mistake 
 // and guess the object is not present in the collection
 
}
```

## Right

[Gist Url]: # (https://gist.github.com/mcsee/4a40df553e8d08860b23f35fb4400c0e)
```java
public class Person {
 
public String name;
// Public attributes are another smell  
 
 @Override
 public boolean equals(Person anotherPerson) {
   return name.equals(anotherPerson.name); 
 }
 	
@Override
 public int hashCode() {
   return name.hashCode(); 
 }
 // This is just an example of non-correlation  
 
}
```

# Detection

[X] Semi-Automatic 

Many linters have rules for hash and equality redefinition.

With mutation testing, we can seed different objects with the same hash and check our tests.

- Identity

- Security

# Conclusion

Every performance improvement has its drawbacks.

Caches and replications are notable examples.

We can (must) use them carefully.

# Relations

%[https://maximilianocontieri.com/code-smell-49-caches]

%[https://maximilianocontieri.com/code-smell-150-equal-comparison]

# More Info

[Equality and Hash](http://forum.world.st/Is-it-always-needed-to-redefine-hash-message-when-you-redefine-message-td4828721.html)

[Hashcode in Java](https://stackoverflow.com/questions/3563847/what-is-the-use-of-hashcode-in-java)

[Hashcode vs Equal](https://www.digitalocean.com/community/tutorials/java-equals-hashcode)

# Disclaimer

Code Smells are just my [opinion](https://maximilianocontieri.com/i-wrote-more-than-90-articles-on-2021-here-is-what-i-learned).

* * *

> This will surprise some of your readers, but my primary interest is not with computer security. I am primarily interested in writing software that works as intended.

_Wietse Venema_
 
%[https://maximilianocontieri.com/software-engineering-great-quotes]

* * *

This article is part of the CodeSmell Series.

%[https://maximilianocontieri.com/how-to-find-the-stinky-parts-of-your-code]
