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

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube