[AccessD] CodePlex WCF Community code snippet

Heenan, Lambert Lambert.Heenan at chartisinsurance.com
Tue May 1 11:03:49 CDT 2012


That's what I suspected. Calling a method of an uninstantiated object give an error, unless the class of object that o1 and o2 are is defined as a Static class.

Which I believe is where the design of the codeplex routine comes from...

        public static bool CompareObjects<T>(T o1, T o2) where T : class
        {
            if ((o1 == null) != (o2 == null))
            {
                return false;
            }

            return (o1 == null) || o1.Equals(o2);
        }

If the first part of the if statement is True then we know that one or the other objects is null, but only one: hence we return false. But if execution reaches the Else part we know that either both objects are null, or both have been instantiated. So we first check if o1 is null:  return (o1 == null), but then if it is not null, we can safely call it's Equals method to see if o2 is Equal:  || o1.Equals(o2);

So I still vote for the codeplex routine.

Lambert :-)



-----Original Message-----
From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Salakhetdinov Shamil
Sent: Tuesday, May 01, 2012 11:16 AM
To: Access Developers discussion and problem solving
Subject: Re: [AccessD] CodePlex WCF Community code snippet

Hi Lambert --

> I wish VB used it as well!!! ;-(
Yep :(

> If o1 is null can you call its Equals method?
No, .Equals method results in runtime error if o1 or o2 is null.

Thank you.

-- Shamil

Tue, 1 May 2012 11:03:03 -0400 от "Heenan, Lambert" <Lambert.Heenan at chartisinsurance.com>:
> Indeed Shamil. I am well aware of "sort circuit evaluation" : I wish 
> VB used it as well!!! ;-(
> 
> One question, in you revision
> 
> public static bool CompareObjects3<T>(T o1, T o2) where T : class {
>     if (o1 == null || o2 == null) 
>         return (o1 == null && o2 == null);
>     return o1.Equals(o2);
> }
> 
> If o1 is null can you call its Equals method?
> 
> Lambert
> 
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com 
> [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of 
> Salakhetdinov Shamil
> Sent: Tuesday, May 01, 2012 10:34 AM
> To: Access Developers discussion and problem solving
> Subject: Re: [AccessD] CodePlex WCF Community code snippet
> 
> Hi Lambert,
> 
> Thank you for your reply. Please note that C# (as well as C and C++) do use so called "short circuit evaluation" (http://en.wikipedia.org/wiki/Short-circuit_evaluation).
> 
> I'd still have the code snippets arranged in the following order (first places in the sequence mean more professional/maintainable/effective code IMO. I can be found (absolutely) wrong in the end of this thread discussion).
> Anybody else?
> 
> 1. CompareObjects4
> 2. CompareObjects3
> 3. CompareObjects5
> 4. CompareObjects2
> 5. CompareObjects
> 
> Code snippets:
>         
> public static bool CompareObjects<T>(T o1, T o2) where T : class {
>     // CodePlex version
>     if ((o1 == null) != (o2 == null)) return false;            
>     return (o1 == null) || o1.Equals(o2); }
> 
> public static bool CompareObjects2<T>(T o1, T o2) where T : class {
>     // 'mere mortals' version
>     if (o1 == null && o2 == null) return true;
>     if (o1 != null && o2 == null) return false;
>     if (o1 == null && o2 != null) return false;
>     return o1.Equals(o2);
> }
> 
> public static bool CompareObjects3<T>(T o1, T o2) where T : class {
>     // Gustav's version
>     if (o1 == null || o2 == null)
>     {
>         return (o1 == null && o2 == null);
>     }
>     else
>     {
>         return o1.Equals(o2);
>     }
> }
> 
> public static bool CompareObjects4<T>(T o1, T o2) where T : class {
>     // Gustav's version refactored
>     if (o1 == null || o2 == null) 
>         return (o1 == null && o2 == null);
>     return o1.Equals(o2);
> }
> 
> public static bool CompareObjects5<T>(T o1, T o2) where T : class {
>     // 'mere mortals' version refactored
>     if (o1 == null && o2 == null) return true;
>     if (o1 == null || o2 == null) return false;
>     return o1.Equals(o2);
> }
> 
> 
> 
> Tue, 1 May 2012 09:11:17 -0400 от "Heenan, Lambert" <Lambert.Heenan at chartisinsurance.com>:
> > Hi Shamil,
> > 
> > I would be inclined to say that the code from codeplex is better. The logic is a little bit obscure, but then professional c/c++/c# codes have always tended to be a tad brief in their style. However a brief period of study makes it clearly correct. 
> > 
> > The reason I would prefer the codeplex code is that it only has 6 comparison operations, where the mere mortals code uses 10. Where there are many thousands of objects being compared in a tight loop that could significantly increase the execution time.
> > 
> > Lambert


More information about the AccessD mailing list