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


  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

Subscribe on YouTube