DevDisasters

Any Way the Wind Blows

With the rate of turnover for developers of Ventozoom's flagship application, some part of the system must be cursed. After Robert joined the team, and seeing how his predecessor handled dictionaries, he can understand.

Since landing at Ventozoom six months back, Robert has been working on a single application -- a rather complex .NET application with a cryptic code base to match. Rumored to be responsible for several employee separations, it's the kind of application that started life as an innocent, quick-and-dirty hack solution for a small group of customers who, with the help of a team of aggressive sales guys, rapidly grew.

And with the number of customers, so grew the requests for new features.

Add to the resulting mix of shifting requirements and feature creep, the usual ingredients that you need to transform a problem into a really big mess crept forth: lack of manpower, lack of competence in the available manpower and, last but not least, lack of time. In other words, over the first few years the product was developed by just a handful of people out of their respective technical depths, with too much work to do and not enough time to do it, or enough sleep for that matter.

But the application makes the company piles of money, so it's OK … right?

Hooray for Hash Codes!
While basic hashing implementation and equality comparison in the Object class does allow containers like the .NET HashSet<> to work with any type without any special preparations to the value class, it's also the source of a myriad of (sometimes subtle) software bugs: If GetHashCode is overridden, a consistent implementation of Equals usually also has to be provided (and vice versa), and if equality depends on the object's state (that is, more often than not) the object has to be immutable (because the hash code has to be).

Of course, not every programmer knows about the subtleties of the special twins GetHashCode and Equals. And why should they? One does not have to burden one's self with all this complexity to simply enjoy the fact that any type can be put inside a HashSet, or used as a key type in a Dictionary -- for that matter -- just like Robert's predecessor apparently did.

When refactoring some old code (or, rather, gently lobotomizing it with a virtual chainsaw), he found this:

Dictionary<Dictionary<string, string>, Dictionary<string, string>> projectAttributes = new Dictionary<Dictionary<string, string>, Dictionary<string, string>>();
...
Dictionary<string, string> newAttributes = ProjectUpdate.GetMappingAttributes(projectObject);
Dictionary<string, string> savedAttributes = ProjectUpdate.SaveMappingAttributes(projectObject);
projectAttributes.Add(newAttributes, savedAttributes);
...
return projectAttributes;

Robert was intrigued. A Dictionary (the .NET version of an associative array) of Dictionary to Dictionary? Or perhaps ... a Meta Dictionary. He had, of course, already seen quite some "interesting" code during his career, but this one was new and he couldn't figure out what the purpose of that construct could possibly be. That is, until Robert found the place where it was used.

Key-Value Pairs FTW!
Turned out, Robert's predecessor had been looking for a tuple data type, and in the good ol' days of the Microsoft .NET Framework 3.5, there was none -- at least not built-in. So, naturally, the opportunity to do your own thing, however that may be, was wide open:

Dictionary<Dictionary<string, string>, Dictionary<string, string>> projectAttributes = 
  Arguments.Attributes as Dictionary<Dictionary<string, string>, Dictionary<string, string>>;
...
foreach (KeyValuePair<Dictionary<string, string>, Dictionary<string, string>> 
  projAttributes in projectAttributes)
{
  newProjAttributes = projAttributes.Key as Dictionary<string, string>;
  savedProjAttributes = projAttributes.Value as Dictionary<string, string>;
}

Robert thought the logic through for a minute -- what is a 2-tuple other than a key-value pair? And what is an associative array other than a list of key-value pairs? And what is a single value other than a list with one entry? (And what is a foreach loop over that one entry other than calling First?)

Looking at it from this angle, it almost becomes the most natural thing to use a Dictionary with precisely one entry as a 2-tuple. Almost.

Again, all this joy is only possible because every .NET type implements GetHashCode. Sometimes, Robert still wonders if the creators of the .NET Framework really thought about what things they would help to let loose on the world of source code.

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