In the suite of redux-style applications we are building in C#, we are making extensive use of value-types, which implies that a value compares as equal exactly if all of its contents are equal also known as “deep equality”, as opposed to “reference equality” or “shallow equality”. Both of those imply deep equality, but the other way around is not true. The same object is of course equal to itself, not matter how deep you look. And an object that references the same data as another object also has equal content. But a simple object that contains different lists with equal content will be unequal under shallow comparison, but equal under deep comparison.
records already provide a per-member comparison as Equals be default, this fails for collection types such as
ImmutableList<> that, against all intuition but in accordance to , only provide reference-equality. For us, this means that we have to override
Equals for any value type that contains a collection. And this is were the trouble starts. Once
Equals is overridden, it’s extremely easy to forget to also adapt
Equals when adding a new property. Since our redux-style machinery relies on a proper “unequal”, this would manifest in the application as a sporadically missing UI update.
So we devised a testing strategy for those types, using a little bit of reflection:
- Create a sample instance of the value type with no member retaining its default value
- Test, by going over all properties and comparing to the same property in a default instance, if indeed all members in the sample are non-default
- For each property, run Equals the sample instance to a modified sample instance with that property set to the value from a default instance.
If step 2 fails, it means there’s a member that’s still at its default value in the sample instance, e.g. the test wasn’t updated after a new property was added. If step 3 fails, the sample was updated, but the new property is not considered in Equals – and it can even tell which property is missing.
The same problems of course arise with
GetHashCode, but are usually less severe. Forgetting to add a property just makes collisions more likely. It can be tested much in the same way, but can potentially lead to false positives: collisions can occur even if all properties are correctly considered in the function. In that case, however, the sample can usually be altered to remove the collision – and it is really unlikely. In fact, we never had a false positive.