DevDisasters

The Long and Short of It

Not just once, but repeated in various lucrative apps -- a humongous chunk of code that should've been reduced to a mere line.

Thomas B.'s employer isn't a company that offers only a single flagship application -- it has dozens!

For years, the company has been making money year after year by developing and maintaining niche applications aimed at specific industries. Basically, it finds an existing application that's lacking a feature that might only be used by a handful of groups in the world and then implements it.

However, despite being lucrative, it's a somewhat chaotic business model, occasionally leading to a few support nightmares. Case in point: One of the company's C# application libraries processes the images inside a PDF and sends them through an automatic color correction library with perhaps 25 users worldwide. The app has been selling to one or two new users each year.

For many companies, this wouldn't be much of a success, but considering these users were large organizations, between the price charged and annual support license renewals, it was a good, stable moneymaker.

Despite these plusses, it was actually architected in perhaps the worst way possible. Buried within the application logic was code that would spawn one thread per image (some PDFs can have several hundreds of images!). There were frequent deadlocks, multiple copies of the same objects in memory for no apparent reason (images and PDFs, so very large objects), and three copies of the PDF on the disk (which can easily be 300MB to 800MB).

The reason behind most of these problems? One function that Thomas B. found while on a refactoring expedition:

private static int GetPhysicalFileSize(byte[] Imgbyte, string InputObject, string OutFolder)
{
  int PhysicalFileSize = 0;

  try
  {
    ExtractImage(Imgbyte, "Photo_" + InputObject + ".jpg", OutFolder);
    var Outfile = OutFolder + "\\Photo_" + InputObject + ".jpg";

    if (File.Exists(Outfile))
    {
      FileInfo fi2 = new FileInfo(Outfile);
      PhysicalFileSize = (int)fi2.Length;
    }

    if (File.Exists(Outfile))
    {
      File.Delete(Outfile);
    }
  }
  catch (Exception ex)
  {
    Trace.WriteError(ex);
  }

  return PhysicalFileSize;

}

So, to determine the size of a byte[] as a file, first, the code would write out the information to disk, read that information and then, finally, delete the file.

All that disk I/O work for the equivalent of this:

return Imgbyte.Length;

Calls to the larger, more cumbersome function were peppered throughout the source, so there was no doubt the handful of customers would be appreciative of the fix, as the benefits would be immediately noticeable.

Thomas B. knew that fixing this bug was a big win, but it was only the tip of the iceberg in the great catalog of apps that he worked on supporting. Thank goodness for steady customers.

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

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

  • Copilot Agentic AI Dev Environment Opens Up to All

    Microsoft removed waitlist restrictions for some of its most advanced GenAI tech, Copilot Workspace, recently made available as a technical preview.

Subscribe on YouTube