[AccessD] CodePlex WCF Community code snippet

Salakhetdinov Shamil mcp2004 at mail.ru
Tue May 1 10:35:30 CDT 2012


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


Tue, 01 May 2012 16:44:24 +0200 от "Gustav Brock" <Gustav at cactus.dk>:
> Hi Shamil
> 
> Yes that's better/simpler.
> 
> The CodePlex solution I didn't like; it does right, of course, but you must read the method twice or more to grasp how it works.
> 
> /gustav
> 

<<< snip >>>



More information about the AccessD mailing list