[AccessD] CodePlex WCF Community code snippet

Gustav Brock Gustav at cactus.dk
Tue May 1 10:55:05 CDT 2012


Hi Shamil

CompareObjects6 looks like the winner. And it is very logical to read as well.

/gustav


>>> mcp2004 at mail.ru 01-05-2012 17:35 >>>
Hi Gustav and Lambert at all:

Have a look I have added one mode 'Mere mortals' version:

C#
==

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

And here are different version decompiled (compiled with 'Optimize code' option set).

What version is the winner IYO? (Do you have any other code snippets for this simple function to analyze?)

Decompiled
==========

// 'Mere mortals' refactored 2  (decompiled)
public static bool CompareObjects6<T>(T o1, T o2)
{
    if (o1 == null || o2 == null)
    {
        return o1 == o2;
    }
    else
    {
        return o1.Equals(o2);
    }
}

// CodePlex  (decompiled)
public static bool CompareObjects<T>(T o1, T o2)
{
    if (o1 == null == o2 == null)
    {
        if (o1 == null)
        {
            return true;
        }
        else
        {
            return o1.Equals(o2);
        }
    }
    else
    {
        return false;
    }
}

// 'Mere Mortals' original  (decompiled)
public static bool CompareObjects2<T>(T o1, T o2)
{
    if (o1 != null || o2 != null)
    {
        if (o1 == null || o2 != null)
        {
            if (o1 != null || o2 == null)
            {
                return o1.Equals(o2);
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }
    else
    {
        return true;
    }
}

// Gustav's original & refactored  (decompiled)
public static bool CompareObjects3<T>(T o1, T o2)
{
    if (o1 != null && o2 != null)
    {
        return o1.Equals(o2);
    }
    else
    {
        if (o1 != null)
        {
            return false;
        }
        else
        {
            return o2 == null;
        }
    }
}


// 'Mere mortals' refactored (decompiled)
public static bool CompareObjects5<T>(T o1, T o2)
{
    if (o1 != null || o2 != null)
    {
        if (o1 != null && o2 != null)
        {
            return o1.Equals(o2);
        }
        else
        {
            return false;
        }
    }
    else
    {
        return true;
    }
}

Thank you.

-- Shamil




More information about the AccessD mailing list