Tuesday, August 7, 2012

How Coding WTFs Happen

I love to laugh at the coding gaffes on TheDailyWTF, but I know that I shouldn't laugh, because I know the coding gaffes could happen to me by chance.  I saw a code snippet like this one awhile back in code I work with.

switch (val)
{
  case ENUM_A:
    do_A();
    break;
  case ENUM_B:
  case ENUM_C:
  default:
    do_default();
    break;
}

I remember this pretty clearly b/c I know exactly how it reached this point.  The code used to be like this:

switch (val)
{
  case ENUM_A:
    do_A();
    break;
  case ENUM_B:
    do_B();
    break;
  case ENUM_C:
    do_C();
    break;
  default:
    do_default();
    break;
}

However, as code was condensed and options eliminated, people removed only the portion of code that was necessary to remove.  Only after looking at the code from a distance does someone finally realize this should be condensed to something far more reasonable (and removing the unnecessary enums too):

if (NEW_BOOL_FLAG)
   do_A();
else
   do_default();

No comments:

Post a Comment