DevDisasters

A Bit Unwise

Was "Calvin code" genius or tomfoolery?

"Wait a second -- only after the price of diesel reaches four bucks a gallon does anybody notice a bug in the fuel efficiency reports?" John C. asked incredulously.

"Yep. We're talking about old Calvin code," his manager said.

John's employer sold vehicle fleet tracking units. The company's founders hired Calvin to write the firmware that ran on the vehicles and the receiving applications on the server side.

Forbidden Territory
John wished that the problem was with the reports, or any part of the code developed after the Calvin era. No such luck: The problem was Calvin's C# server-side code -- specifically, the part responsible for determining the engine's running status.

Everybody agreed that Calvin was smart, but he liked to show off.

Why limit yourself to true and false when magic numbers like 319 and 329 would do? And despite his "wizard" status, he seemed to have a tenuous grip on the concepts of binary, hex and bitwise logic.

After tracking down the code responsible for detecting the ignition status, John knew he would be in for a headache later on:

public string inputDescription(int iDec) {
  int numbase = 2, base10 = 10, MaxBit = 32;
  if (iDec == 0)
    return "Engine OFF";
  char[] cHexa = new char[] { 'A', 'B', 'C', 'D', 'E', 'F' };
  int[] iHexaNumeric = new int[] { 10, 11, 12, 13, 14, 15 };
  int[] iHexaIndices = new int[] { 0, 1, 2, 3, 4, 5 };
  string strBin = "", description = "Engine OFF";;
  int[] result = new int[32];
  for (; iDec > 0; iDec /= numbase)
  {
    int rem = iDec % numbase;
    result[--MaxBit] = rem;
  }
  for (int i = 0; i < result.length;="" i++)="" if="" ((int)result.getvalue(i)="">= base10)
      strBin += cHexa[(int)result.GetValue(i) % base10];
    else
      strBin += result.GetValue(i);
  strBin = strBin.TrimStart(new char[] { '0' });

  bool ignition = false, input4 = false, input5 = false;
  
  if (strBin.Length == 2 || strBin.Length > 2)
    if (strBin.Substring(strBin.Length - 2, 1) == "1")
      ignition = true;
    else
      ignition = false;
  if (strBin.Length == 4 || strBin.Length > 4)
    if (strBin.Substring(strBin.Length - 4, 1) == "1")
      input5 = true;
    else
      input5 = false;
  if (strBin.Length == 5 || strBin.Length > 5)
    if (strBin.Substring(strBin.Length - 5, 1) == "1")
      input4 = true;
    else
      input4 = true;
  
  if (input4 == true && input5 == true && ignition == false)
    description = "Engine Stopped, Oil Pressure Low (Mechanical Failure)";
  else if (input4 == false && input5 == true && ignition == false)
    description = "Engine Stopped, Ignition ON (High water temperature)";
  else if (input4 == true && input5 == false && ignition == false)
    description = "Engine Switched OFF";
  else if (input4 == true && input5 == true && ignition == true)
    description = "Engine Runnning";
  
  return description;
}

Real Magic
John dove into the logic as pressure from the customers mounted. Every time he made some headway on regression testing -- different vehicles had their own magic numbers, of course -- something would break. On the verge of throwing his PC out of the nearest window and making a break for it, John experienced a breakthrough. Could there have been some real genius after all in Calvin's magic numbers?

John grabbed a calculator and a list of ignition codes, and sure enough, the solution was easy. Calvin had just taken the long route:

public static string inputDescription (int iDec)
{            
  bool ignition = (iDec & 2) != 0;
  bool input4 = (iDec & 8) != 0;
  bool input5 = (iDec & 16) != 0;

  String description = "Engine OFF";

  if (input4 && input5 && !ignition)
    description = "Engine Stopped, Oil Pressure Low (Mechanical Failure)";
  else if (!input4 && input5 && !ignition)
    description = "Engine Stopped, Ignition ON (High water temperature)";
  else if (input4 && !input5 && !ignition)
    description = "Engine Switched OFF";
  else if (input4 && input5 && ignition)
    description = "Engine Running";

  return description;
}

After testing, John was satisfied his own "magic" had won the day. Unfortunately, that was only one function down -- there were hundreds to go.

About the Author

Mark Bowytz is a contributor to the popular Web site The Daily WTF. He has more than a decade of IT experience and is currently a systems analyst for PPG Industries.

comments powered by Disqus

Reader Comments:

Add Your Comment:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above