CodeSOD: The GUID Utillity

Let's say you saw a method called StrToGuid, in a C# codebase. Your first thought might be: "Wait, isn't there a built in parse? Well, I guess maybe they might do some sort of exception handling. But it still doesn't seem right." And then you'd take a look at the method signature and see that it takes both a string, and an integer named counter, and you'd think: "Wait, what?"

Henrik H had a similar experience. His team hired a new developer, someone with 15+ years of experience. This is what they contributed to the codebase:

private Guid StrToGuid(string s, int counter) { Guid newGuid = new Guid(); if (counter < 10) Utillity.ScreenAndLog("d", s); try { newGuid = Guid.Parse(s); _noOfOKGuids++; } catch(ArgumentNullException) { _noOfEmptyGuids++; } catch(FormatException) { _noOfErrorGuids++; } return newGuid; }

So, if s contains a valid GUID, this parses it into a GUID and returns it. Otherwise, it returns a new GUID. It also accepts that counter parameter which clearly exists to log every tenth GUID we attempt to parse, using the "Utillity"(sic) class. Instead of responding to exceptions, we just increment counters- counters which never get checked by any other method, by the way, so this is essentially just "swallowing the exceptions", as it were.

Henrik adds: "Yes, he is related to the boss."

[Advertisement] Continuously monitor your servers for configuration changes, and report when there's configuration drift. Get started with Otter today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.