TDD with .NET - null coalescing for defaults

By: Johnathon Wright on: December 19, 2008

Yet another post in the  "TDD with .NET":  series... 

There are some fun tricks that can make code a little bit more elegant for advanced coders, though newer developers will take some adjusting.

Suppose there is a business rule that when a name isn't valid (as defined by is_valid), we should display 'unknown.'

I've created a Name class, which stores a FirstName, MiddleInitial and LastName. I have a property named FullName that returns a string.

---csharp namespace EduTest{     [TestFixture]     public class NameTests     {         [Test]         public void FullNameisnullifincomplete()         {             Name mick = Factory.Name;             mick.FirstName = null;             Assert.Equals(null, mick.FullName);         }     }


seven tests, one failure ---csharp public string FullName{     get     {         if (this.isvalid)         {             StringBuilder fullname = new StringBuilder();             fullname.Append(FirstName);             if (MiddleInitial.HasValue)             {                 fullname.Append(' ');                 fullname.Append(MiddleInitial);                 fullname.Append('.');             }             fullname.Append(' ');             fullname.Append(LastName);             return full_name.ToString();         }         else         {             return null;         }     }


seven tests, zero failures

This method is long-ish. I would like to refactor it, but I'll hold off for now. I'm also wondering whether this logic actually belongs in this method... but without any feedback from another developer or a good idea to persue, I'm going to leave it alone until it becomes painful or I think of a better option. Next test...

namespace EduTest{     [TestFixture]     public class NameTests     {         [Test]         public void ToStringisUnknownwhennameisincomplete()         {             Name prince = new Name();             prince.FirstName = "Prince"             prince.LastName = null;             Assert.Equals("Unknown", prince.ToString());         }     }


eight tests, one failure

Now I'm going to use the cool "null-coalescing operator": ( ?? ).

---csharp namespace Edu{     public class Name     {         public override string ToString()         {             return FullName ?? "Unknown"         }     }


The null coalescing operator allows you to specify what value you want used if the first value is null. "hello" ?? "value" results in "hello"null ?? "default" results in "default"

eight tests, zero failures


Just checking that you are human. What would be the result of this code?

a = 3*(4/2); b = 1; a+b