[AccessD] CodePlex WCF Community code snippet

Salakhetdinov Shamil mcp2004 at mail.ru
Wed May 2 07:23:14 CDT 2012


Hi Gustav --

>  let me put the question why you pay so 
> much attention to this tiny detail?
Just wanted to learn a bit from others' coding and immediately got at

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

And that is coming from MS AFAIU.

And so I decided to ask AccessD members' opinion how (useful) they find the above code: I wondered if one would have to support others code written in such a style for functions having not two but twenty+ lines, many functions like that, would they find such a job easy?

And then as we together found (but Lambert ins't in our camp yet :) ) the following variant is more straightforward:

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 so easier to understand for "strangers" and to support...

> If you evaluate all your code this way you would probably starve!
I almost don't do such a thorough evaluation of course, no time during everyday "code crunching" programming work...
Just sometimes when getting at program performance issues I'm reevaluating and redoing  coding to make it flying, and it works usually.

>  even for a million iterations you most likely wouldn't be able 
> to notice any difference in execution time.
For this certain case yes, the difference can be neglected, but similar code involving some indirect (lazy) intitialization / (external) data loading before actual comparison is done could be rather time consuming for every comparison and so it would matter how much of comparisons are actually done till a function will return its result...

For the above function we have to check that o1 and o2 are not null before calling .Equals(...) method so I still can't get why folks have written such a strange code, can you?

Feel free to accept the last as rhetoric question...

Thank you.

-- Shamil

Wed, 02 May 2012 08:40:17 +0200 от "Gustav Brock" <Gustav at cactus.dk>:
> Hi Shamil
> 
> Yes, that's strange. 
> But, just curious, let me put the question why you pay so much attention to this tiny detail? If you evaluate all your code this way you would probably starve! I mean, even for a million iterations you most likely wouldn't be able to notice any difference in execution time.
> 
> /gustav
> 

<<< snip >>> 



More information about the AccessD mailing list