Salakhetdinov Shamil
mcp2004 at mail.ru
Tue May 1 10:13:15 CDT 2012
Hi Gustav --
Have a look - I have got decompiled the original code using Telerik JustDecompile (compilation of original code was done using VS2010 with 'Optimize code' option set to false):
- I'd still put your code version on first place (both original and refactored versions produce the same executable code internally), CodePlex version - on second and 'Mere mortals' refactored version - now on third. (Original 'mere mortals' version is out of game now as very ineffective).
// Gustav (decompiled)
public static bool CompareObjects4<T>(T o1, T o2)
{
bool flag;
bool flag1;
bool flag2;
if (o1 == null)
{
flag1 = false;
}
else
{
flag1 = o2 != null;
}
bool flag3 = flag1;
if (flag3)
{
flag = o1.Equals(o2);
}
else
{
if (o1 != null)
{
flag2 = false;
}
else
{
flag2 = o2 == null;
}
flag = flag2;
}
return flag;
}
// CodePlex (decompiled)
public static bool CompareObjects<T>(T o1, T o2)
{
bool flag;
bool flag1;
bool flag2 = o1 == null == o2 == null;
if (flag2)
{
if (o1 == null)
{
flag1 = true;
}
else
{
flag1 = o1.Equals(o2);
}
flag = flag1;
}
else
{
flag = false;
}
return flag;
}
// 'Mere mortals' (decompiled)
public static bool CompareObjects5<T>(T o1, T o2)
{
bool flag;
bool flag1;
bool flag2;
if (o1 != null)
{
flag1 = true;
}
else
{
flag1 = o2 != null;
}
bool flag3 = flag1;
if (flag3)
{
if (o1 == null)
{
flag2 = false;
}
else
{
flag2 = o2 != null;
}
flag3 = flag2;
if (flag3)
{
flag = o1.Equals(o2);
}
else
{
flag = false;
}
}
else
{
flag = true;
}
return flag;
}
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
>
>
> >>> mcp2004 at mail.ru 01-05-2012 16:22 >>>
> Hi Gustav,
>
> Very good!
>
> I'd just have refactored it a bit:
>
> 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);
> }
>
> Thank you.
>
> -- Shamil
<<< snip >>>