Laurent Hinoul

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - John F. Woods

How to find the similarity rate of two objects

How can you find the similarity rate of two objects expressed by a percentage? Read it below.

For example, I have two object with the same object type. In this example we have an object ‘contact’.

Contact model class

Now, lets say you have two contact objects:

Contact object 1

  • lastname: null
  • firstname: Laurent
  • language: NL

Contact object 2

  • lastname: Hinoul
  • firstname: Laurent
  • language: NL

Now we want to know what the equality is between the two classes is, expressed in a percentage. To do so, we make use of Reflect. Copy the following class in your code;

Now we can test our code. Implement the following test case:

The result is that the two object are 66% equal.

Leave a Reply