Two Wrongs

Do Objects Have Natural Keys?

Do Objects Have Natural Keys?

In many popular languages, all objects have a notion of natural equality. They support a method named something like Equals which compares them to any other object. By default, this method tends to use reference equality, but it doesn’t have to11 For example, structs in C# (being value types) by default compare the values of their public fields..

It’s not impossible to compare objects by some other measure: whenever objects are to be compared, you can often supply some sort of comparator22 In Java, this is the aptly named Comparator. that will compare objects in the way specific to that comparator. But this does not change the semantics of the Equals method, which still implements the natural equality measure of the object.

Abuse of Natural Equality

The natural equality of objects dictate, for example, how the objects behave when you put them into sets (unless the set is constructed with a different comparator, that is). If two objects are naturally equal, only one of them will be included in the set.

Sometimes, I want only a subset of the public members of an object .

Idea: Natural Key