POCO comparison discussion

How would you compare two POCO‘s, which are instances of the same type?

Assuming you only want to compare the public properties, which can be simple or complex types.

While discussing this shortly with a good friend, we came up with three naive options.
You can check them out on GitHub, but i’ll summarize them here:

  1. Manually compare properties
    1. I don’t think this is very future proof or maintenance friendly, because adding a property but forgetting to update the comparison algorithm  is very real (as a professional mender I have a sixth sense for this kind of things)
  2. Compare serialized JSON string
    1. We didn’t come up with this, but kind of encountered this in production code. I think this solves the future proofing, but i’m wary of the performance implications.
  3. Compare via reflection
    1. This sounds obvious, we did wonder how far this rabbit hole goes when you have properties with a complex type, we foresee some comparison challenges.

Send a pull request if you got a crazy idea.

Theme music for this blog post

POCO comparison discussion

3 thoughts on “POCO comparison discussion

  1. Personally, I would go for the serialization 100% of the time. Is it the fastest? Definitely not, but’s 80% of the result in 20% of the effort. And I’m a lazy coder.
    You could also implement an .Equals(T obj) from the IEquatable interface for every Type involved, however that creates overhead of having to maintain the equality-check when the POCO’s change, making it more prone to errors. (as you said)
    Hashing objects “should” also yield the same results for two equal objects. But I wouldn’t use that in production or critical code paths.
    I guess it depends on how (time) critical the equality check is.

    Liked by 1 person

    1. Interesting comment about the hashing, because I only learned that recently 🙂
      That’s actually the complete solution I encountered in option 2, first serialize to JSON strings and then compare their hashes.

      While questioning that implementation (spidey senses tingling), I read the MSDN docs and only then learned that equal strings are guaranteed to have the same hash..but different strings are not guaranteed to have different hashes.

      By chance my friend is doing a deep dive in to .NET and looking into GetHashCode, and shared a post on the origins of GetHashCode.
      Turns out it was only ever intended for…storing the object in a hash table, hehe.


  2. commentator2016 says:

    LINK : http://codinghelmet.com/?path=howto/implement-icloneable-or-not

    Why There is No Deep Cloneable Interface?

    On a zero-to-infinity numeric scale it is quite simple to say which is the smallest number: it is number zero. But what is the largest number? That question cannot be answered because for any given number there is a number by one larger than that. There is no largest number on an open scale.

    The same is true in cloning business. It’s easy to say what is the shallow copy – it is already provided for us in Object.MemberwiseClone method. There is nothing special to do to have shallow copy of an object. But deciding what is a true deep copy may be very tricky. Let’s take a look at several examples.

    What is the deep copy of a collection? Does it require to recursively copy elements in the collection? And what if some of the contained objects do not implement deep cloneable interface? Should the operation fail, or silently step over those elements?

    What is the deep copy of a circular list? How can we discover that some contained object has already been cloned, just to pick the same reference on all places where original object was used? Without this precaution, we run into risk of making an infinite recursion, which typically turns into a stack overflow exception with immediate notice.

    What is the deep copy of an object with external dependencies? Should HttpWebRequest clone the accompanying Web server, just to make sure that it is another Web server this time? Should Stream class copy all file contents into a spare location just to make sure that it’s not the same data as in original stream?

    Questions about really-deep-cloning are endless. There seems to be no proper way to define deep cloning without getting into trouble of defining a cutting edge between cloning the object at hand and cloning other objects which would have to remain invariant with respect to the cloned object. Consequently, deep cloneable interface is not part of Base Class Library.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s