[AccessD] CodePlex WCF Community code snippet

Salakhetdinov Shamil mcp2004 at mail.ru
Tue May 1 09:33:52 CDT 2012


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
> 
> -----Original Message-----
> From: accessd-bounces at databaseadvisors.com [mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Salakhetdinov Shamil
> Sent: Tuesday, May 01, 2012 8:34 AM
> To: Access Developers discussion and problem solving
> Subject: [AccessD] CodePlex WCF Community code snippet
> 
> Hi All --
> 
> Sorry I'm posting C# code snippet here as my question/quick poll is more about coding style than anything else:
> 
> I have occasionally got browsing through the following CodePlex WCF Community source code:
> 
> http://wcf.codeplex.com/SourceControl/changeset/view/66aa503c963c#WCFJQuery%2fTest%2fMicrosoft.Runtime.Serialization.Json.FunctionalTests%2fCommon%2fUtil.cs 
> 
> ...
>         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);
>         }
> ...
> 
> And I have got stuck first trying to get its logic.
> 
> Would you consider the above code more professional and maintainable and effective than the following "mere mortals" code version? If Yes/No - why?
> 
>         public static bool CompareObjects2<T>(T o1, T o2) where T : class
>         {
>             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);
>        }
> 
> Thank you ;)
> 
> -- Shamil 
> 
> 
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 
> -- 
> AccessD mailing list
> AccessD at databaseadvisors.com
> http://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
> 



More information about the AccessD mailing list