Legacy code horror

When handling legacy code, imagine having to refactor someone else’s code that is mainly built up on structures like this:

switch (state)
{
   case ApplicationStateTimeStampType.Imported:
      if (p == false)
      {
         if (!applicationLoading)
            tblstate.importStartedTimeStamp = DateTime.Now;
      }
      else
      {
         if (!applicationLoading)
            tblstate.importedTimeStamp = DateTime.Now;
      }
      break;
 
   case ApplicationStateTimeStampType.Validated:
      if (p == false)
      {
         if (!applicationLoading)
            tblstate.validateStartedTimeStamp = DateTime.Now;
      }
      else
      {
         if (!applicationLoading)
            tblstate.validatedTimeStamp = DateTime.Now;
      }
      break;
 
   case ApplicationStateTimeStampType.Transformed:
      if (p == false)
      {
         if (!applicationLoading)
            tblstate.transformStartedTimeStamp = DateTime.Now;
      }
      else
      {
         if (!applicationLoading)
            tblstate.transformedTimeStamp = DateTime.Now;
      }
      break;
}
 
tblstate.changedTimeStamp = DateTime.Now;

Then, consider that the method is called from functions that set loading to true, which means that only the last line gets executed.

Advertisements