question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Usage of hash code causes comparison to return wrong result

See original GitHub issue

It seems like the code uses hash codes to prevent cycles during the comparison of objects. The hash code is used as a key for a dictionary. I think this is not a good idea because hash codes do not have to be unique. It is absolutely valid for two different objects to have the same hash code. We actually have exactly a situation where comparison fails for this reason.

Here is some example code which should show the problem (the test fails)

[TestClass]
public class TestHashCode
{
    [TestMethod]
    public void Test_SameHashCode()
    {
        Details details1 = new Details { fooType = new FooType { typeId = 1 }, additionalInfo = "info" };
        Details details2 = new Details { fooType = new FooType { typeId = 2 }, additionalInfo = "info" };

        CompareLogic logic = new CompareLogic();
        ComparisonResult cmpResult = logic.Compare(details1, details2);

        Assert.IsFalse(cmpResult.AreEqual);
    }
}

public class FooType
{
    public int typeId;

    public override int GetHashCode()
    {
        return typeId;
    }

    public override bool Equals(object obj)
    {
        FooType other = obj as FooType;
        if (other == null)
            return false;

        return this.typeId == other.typeId;
    }
}

public class Details
{
    public FooType fooType;
    public string additionalInfo;

    public override int GetHashCode()
    {
        return this.fooType.GetHashCode();
    }

    public override bool Equals(object obj)
    {
        Details other = obj as Details;
        if (other == null)
            return false;

        if (this.fooType.Equals(other.fooType) == false)
            return false;

       return this.additionalInfo.Equals(other.additionalInfo);
    }
}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
GregFinzercommented, Mar 6, 2017

No thank you.

0reactions
sbradlcommented, Mar 6, 2017

Don’t you even want to give a reason why you won’t fix it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can hashCode() return the same value for different ...
hashing an object means "finding a good, descriptive value (number) that can be reproduced by the very same instance again and again".
Read more >
The 3 things you should know about hashCode()
So you will have equal objects with different hash codes. For example, calling contains() on a HashMap will return false, even though the...
Read more >
Java equals() and hashCode()
If we only use hashCode() and don't implement equals() then also value will be not retrieved because equals() method will return false.
Read more >
How (not) to break your app with hashCode() and equals()
The basic rule of the contract states that if two objects are equal to each other based on equals() method, then the hash...
Read more >
Java hashCode() and equals() Methods
Most Java classes override this method to provide their own comparison logic. hashcode() – returns a unique integer value for the object in ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found