[AccessD] CodePlex WCF Community code snippet

Salakhetdinov Shamil mcp2004 at mail.ru
Tue May 1 13:16:33 CDT 2012


> So I still vote for the codeplex routine.

OK, but for

if ((o1 == null) != (o2 == null))

"short circuit evaluation" can't be applied for the case when o1 != null and o2 != null, can it be?

What about this version...

public static bool CompareObjects6<T>(T o1, T o2) where T : class
{
    // 'mere mortals' version refactored 2
    if (o1 != null && o2 != null) return o1.Equals(o2);
    return (o1 == o2);
}

...or it's variation?:

public static bool CompareObjects7<T>(T o1, T o2) where T : class
{
    // 'mere mortals' version refactored 3
    if (!(o1 == null || o2 == null)) return o1.Equals(o2);
    return (o1 == o2);
}

They use more "straightforward" logic than CodePlex version, don't they?

And Gustav's version:

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);
}

uses just one "parasite" - o1 == null comparison for the case when o1 != null and o2 != null (the most(?) applicable case).

And that Gustav's version can be further optimized:

public static bool CompareObjects8<T>(T o1, T o2) where T : class
{
    // Gustav's version refactored 2
    if (o1 == null || o2 == null) return (o1 == o2);
    return o1.Equals(o2);
}

What about that version is looks like an absolute favorite one? ;)

Thank you.

-- Shamil

Tue, 1 May 2012 12:03:49 -0400 от "Heenan, Lambert" <Lambert.Heenan at chartisinsurance.com>:
> 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 :-)
> 
> 
<<< snip >>>



More information about the AccessD mailing list