Algirdas Kepezinskas was debugging some code at a client and came across the most treacherous date manipulation algorithms he's ever seen. I've removed the name of the function and will leave it as an exercise to the reader to try to figure out what this huge heap of arithmetic does ...

private int ?????? (Date date1, Date date2)
{
  int retval = 0;
  if (date1 > date2) return 0;

  int d1 = date1.Year * 10000 + date1.Month * 100 + date1.Day;
  int d2 = date2.Year * 10000 + date2.Month * 100 + date2.Day;
  int dat = d1 / 100;
  int a = 0, d = 0;

  while ((dat <= (d2 / 100)) && (a < 12))
  {

    int daymo = 31;
    if (dat/100 == 2) if ((dat%100 %4)==0) daymo -= 2; else daymo -= 3;
    if ((dat/100 % 2)==(dat/100 / 8)) daymo -= 1;

    if ((d1/100 == dat) || (d2/100 == dat))
    {
      if (d1/100 == dat) d = daymo - (d1%100)+1;
      if (d2/100 == dat)
      {
        if (d1/100 == d2/100) d -= daymo - d2%100;
        else d = d2%100;
      }
    }
    else d = daymo;

    retval += d;

    if (dat%100 == 12) dat = (dat/100 + 1)*100 + 1;
    else dat += 1;

    a +=1;
  }

  return retval;
}

Get up yet? I'll furnish a clue -- the single line that Algirdas replaced the function with:

return (date1 - date2);

In that particular language, a subtraction of two dates returned the number of days apart. Some how, this function managed to do the same thing.

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