My own Private WTF

I've always wanted to submit something to The Daily WTF (come to think of it, I think I did but that was a long time ago) but today it just made me cry as I experienced my own private WTF. We had a developer leaving today and as I was doing a code sweep (looking at what was there, how the domain was shaping up, etc.) I came across this gem:

    5         private enum DumbLevel
    6         {
    7             Dumb,
    8             Dumber,
    9             Dumbest,
   10             Not
   11         }
Okay, I said to myself. It's his last day, he's having some fun. Back when I worked with wood burning computers I wrote code with silly variable names too.

Then of course my curiosity was piqued and I just had to know how this enum was being used. This led me to this snippet:

   19             if (cableCount == 0)
   20             {
   21                 if (cableCount == 1)
   22                 {
   23                     if (PassesCount > 1)
   24                     {
   25                         if (i <= (_segments.Count - 2)) //for all but last
   26                             dumbLevel = DumbLevel.Dumbest;
   27                         else dumbLevel = DumbLevel.Dumb; //last segment
   28                     }
   29                     else dumbLevel = DumbLevel.Dumb;
   30                 }
   31                 else dumbLevel = DumbLevel.Dumber;
   32             }
   33             else
   34             {
   35                 dumbLevel = DumbLevel.Not;
   36             }
   37 
   38             //calculate cable count and heat segment voltage based on the segment configuration
   39             switch (dumbLevel)
   40             {
   41                 case DumbLevel.Dumb:
   42                     cableCount = segment.GetCableCount(); //use # of tracers to determine cable count 
   43                     AuditStep("Cable Count: {0} (based on count of Tracers identified)", cableCount);
   44                     AuditStep("");
   45                     heatSegmentVoltage = SupplyVoltage*Project.VoltageDrop*segmentPercentage;
   46                     AuditStep("Supply Voltage ({0}) * Voltage Drop ({1}) * Segment Percentage ({2})", SupplyVoltage,
   47                               Project.VoltageDrop, segmentPercentage);
   48                     break;
   49 
   50                 case DumbLevel.Dumber:
	           // code omitted for sanity
   51                     break;
   52             }

This was by far the ugliest code I've seen on this project. The if statements alone started my blood to boil, but then when I started to see the case statements my brain turned to mush and that was my day done.

Sigh. Oh well, next week I'll introduce the team to the concept of inheritance so they can see how to make the case statements (and the craziest set of Enum values I've ever seen) go away. And Donald thinks he has it rough hiring new guys?

NOTE: sorry about the formatting, my VS settings are just hosed today.

7 Comments

  • It's certainly the ugliest formatted code I've seen... WTF? ;)

  • THe code is hardly readable both color wise and format wise. can you fix it?

  • Better than the Daily WTF, show an elegant re-implementation or refactoring.

  • Man, I thought it was hard to read this code in your feed (got smushed all in one line), but the coloured on black is just nasty.

  • I agree with David Starr. Please show the "better" way to write this code so we can learn from it.

  • Sure, the case statement could be a strategy, etc. etc. How come no one has pointed out what to me is the most obvious WTF???

    if (cableCount == 0)

    {
    if (cableCount == 1)
    {

    Am I not mistaken, or is the code inside the nested if statement *NEVER* going to get executed??

  • @Justice: Actually that was me when I reformatted the code for the blog. The original code was trimmed but I just threw something in to make it line up correctly. In reality, there's some other conditional check so the inner if statement does get executed.

    As others have posted, I'll post a refactoring to show the result of what should be done in this case.

Comments have been disabled for this content.