Today's Code Snippet comes to us from S.G, who writes, "Over time all development teams inevitably gather their share of WTF code; at the very large project I am currently working on, they hand out "Van Gogh" awards for the best. I hope some of these candidates give you a laugh."

 

The long way around.

Modern language frameworks provide many useful common functions -- sometimes, however, developers may not be familiar with what is available and end up (unnecessarily) writing their own versions of these functions.

One example I came across in the code is below:

 hoursSinceLastRefresh = Convert.ToDecimal(DateDiff("m",DateTime.Now,cachedOn ) / 60);

 ...

private double DateDiff(string howtocompare, System.DateTime startDate, System.DateTime endDate)  {
   double diff=0;
   try
   {
     System.TimeSpan TS = new System.TimeSpan(startDate.Ticks-endDate.Ticks);

     #region converstion options
     switch (howtocompare.ToLower())
     {
       case "m":
         diff = Convert.ToDouble(TS.TotalMinutes);
         break;
       case "s":
         diff = Convert.ToDouble(TS.TotalSeconds);
         break;
       case "t":
         diff = Convert.ToDouble(TS.Ticks);
         break;
       case "mm":
         diff = Convert.ToDouble(TS.TotalMilliseconds);
         break;
       case "yyyy":
         diff = Convert.ToDouble(TS.TotalDays/365);
         break;
       case "q":
         diff = Convert.ToDouble((TS.TotalDays/365)/4);
         break;
       default:
         //d
         diff = Convert.ToDouble(TS.TotalDays);
         break;
     }
     #endregion
   }
   catch
   {
     diff = -1;
   }
   return diff;
 }

Note that as impressive as this function is, it still doesn't actually give the answer the budding artist was after (hours), so they had to use it to calculate minutes and then divide by 60.

Another interesting feature of the code is what appears to be a bug in handling case "q" -- you might be expecting quarters, but in fact the result will be the number of 4 year periods (leap years?).

Of course the function was private, and never used anywhere else, so that branch of the case statement (along with most others) would never actually be called...

The replacement code:

hoursSinceLastRefresh = Convert.ToDecimal((DateTime.Now - cachedOn).TotalHours);

Reflection

Another nomination, found by another team member, was a great demonstration of the reflection features of .NET. Presumably the developer had heard about reflection and thought it was a great way to do things:

if (p.GetType().ToString() ==
"Cxxxx.Cxxxx.Cxxxx.Wxxxx.AxxxxMxxxxBxxxx")
 {
   Type myType = p.GetType();
   PropertyInfo[] props =
myType.GetProperties(BindingFlags.Public|BindingFlags.Instance);
   for (int i = 0; i < props.Length; i++)
   {
     if (props[i].Name == "AppStateId")
     {
       long val = Convert.ToInt64(props[i].GetValue(p, null).ToString());
       _appState =  (enmCCLAppState) val;
       break;
     }
   }
 }

After studying the above code for a while to figure out what it did, the discoverer couldn't help but rewrite into a more efficient way to access a property:

if (p is AxxxxMxxxxBxxxx)  {
   _appState = (enmCCLAppState)((AxxxxMxxxxBxxxx)p).AppStateId;
 }

[Note: Revealing class names have been replaced with xxx's -- the original developer may have made some mistakes, but, at least in this case, really bad names was not one of them.]

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!