:::: MENU ::::

Monday, August 10, 2009

Yesterday evening at work a team member and I were pair programming.  We had a disagreement about how to code a few lines.  The question was around whether to use the ?? operator or to use an if statement using String.IsNullOrEmpty.  We settled it like most developers do and that is with a benchmark.  Here’s the fun we had.

To give you an idea as to what we were doing here’s some context.  We had a function that took an object.  We wanted to add extra context data to the object, but only if the object didn’t have it set.  In other words, the data may have already been set somewhere else in the application and we didn’t want to override what was already set.  There were two ways to do this and since I was typing I started typing the following.

   1: private void AddContextData(LogEntry entry)
   2: {
   3:     entry.Url = entry.Url ?? HttpContext.Current.Request.Url.ToString();
   4:     entry.UserIpAddress = entry.UserIpAddress ?? HttpContext.Current.Request.UserHostAddress;
   5:     entry.UserName = entry.UserName ?? HttpContext.Current.User.Identity.Name;
   6: }

My pairing partner started in on me immediately.  He was thinking it should be written like this.

   1: private void AddContextData(LogEntry entry)
   2: {
   3:     if (String.IsNullOrEmpty(entry.Url)) entry.Url = Context.Current.Request.Url.ToString();
   4:     if (String.IsNullOrEmpty(entry.UserIpAddress)) entry.UserIpAddress = HttpContext.Current.Request.UserHostAddress;
   5:     if (String.IsNullOrEmpty(entry.UserName)) = entry.UserName = HttpContext.Current.User.Identity.Name;
   6: }

The ?? for those that aren’t familiar with it is called the “null coalescing” operator. Scott Gu wrote about it late 2007 if you want some additional information and samples.  It is a fun little operator and can save you a lot of typing in more places than you think.  Most developers though don’t think to use it and instead code the long handed version doing a check using a if block. 

In the end we decided to settle our disagreement and go with the one that was the fastest.  Thus we whipped up a quick benchmark to test one vs. the other.  Here’s the benchmark code for both samples.

?? Operator Benchmark Code

   1: string x = "foo bar";
   2: Stopwatch watch = new Stopwatch();
   3: long ticks = watch.Time(() => x = x ?? "bar foo", 100000);
   4: textBox1.Text += "??: " + ticks.ToString() + Environment.NewLine;

 

Note: If you copy the above code, it will not work on your machine unless you have a Time() extension method as part of your arsenal. 

if { } Benchmark Code

   1: string x = "foo bar";
   2: Stopwatch watch = new Stopwatch();
   3: long ticks = watch.Time(() =>
   4:                             {
   5:                                 if (String.IsNullOrEmpty(x))
   6:                                 {
   7:                                     x = "bar";
   8:                                 }
   9:                             }
  10:                 , 100000);
  11: textBox1.Text += "if: " + ticks.ToString() + Environment.NewLine;

The Results

The results were not all that exciting. Really it only proved there was wasn’t any difference in speed, if anything giving a very very slight edge to the ?? operator.  At any rate it was a fun side bar to end the evening.  Happy null coalescing!