Thursday, October 23, 2008

 

Do You Review?

I can honestly say I love where I work. And today was a classic example why. I had a code review! (does your team have code reviews?). One of my colleagues pointed out I could make use of Nullable GetValueOrDefault() in the following code snippet:

Instead of this:

int? objectID;          // passed in to a method...
DateTime? signedDate;   // --- "" ---
 
if (objectID == null)
{
    objectID = 0;
}
 
if (signedDate == null)
{
    signedDate = (DateTime)SqlDateTime.MinValue;
}
 
SomeDBWrapperMethod((int)objectID, (DateTime)signedDate);


Just do this:

SomeDBWrapperMethod(objectID.GetValueOrDefault(), 
                    signedDate.GetValueOrDefault((DateTime)SqlDateTime.MinValue));

Seems so obvious, after the fact! I’m sure I must have come across this before, but I can’t remember having ever used it. It’s great to have extra pairs of eyes go over your code.


    

Powered by Blogger