CS101 – Whats wrong with this code?

March 2, 2006

Today I stumbled across something similar to the following chunk of c# code.

   19         private static void DoStuff(object o)
   20         {
   21             // Do Some Work...
   22             object newObject = CheckObject(o);
   23             // More Work...
   24         }
   25
   26         private static object CheckObject(object param)
   27         {
   28             if (!(param == null))
   29                 return param;
   30             else
   31                 return null;
   32         }

There is something wrong hereā€¦

  1. 5 points for leaving a comment with a more readable implementation.
  2. 5 points bonus points for also telling me what language the original author of this code most feels at home writing.

Interestingly the .net 1.1 c# compiler does not automatically optimize this code, I wonder why?

Disclaimer: Points cannot be exchanged for cash. Points have no material value and are actually worthless.