CodeSOD: Nullable Booleans

Austin's team received a bug report. When their C# application tried to load certain settings, it would crash. Now, since this was a pretty severe issue in their production software, impacting a fair number of customers, everyone on the team dove in.

It didn't take Austin long to spot the underlying problem, which isn't quite the WTF.

bool? setting; public static bool GetSetting(){ return (bool)setting; }

setting is defined as nullable, so when attempting to cast to bool, a null value will fail. Theoretically, this should have been caught by testing, but at least the fix was easy. Austin simply added a coalescing operator to the return line: return setting ?? false.

While Austin sent out a pull request, a second pull request came in. This one was from Austin's boss, who was far more experienced- and who had been gifted the ability to approve their own pull requests. So this is what they approved, a change in where GetSetting is called:

try{ checkbox.Checked = Foo.GetSetting(); } catch{}

While Austin took a moment to understand the root cause of the issue, his boss took the straight line path: "this line throws an exception, so let's just make the exception go away".

After some convincing, the team agreed that the boss's commit should be rolled back and an actual proper null handling was a better choice. That didn't stop the boss from grumbling that it was an emergency, and it was more important to get it fixed fast than fixed right. Austin didn't point out that he submitted his pull request first.

[Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!

This post originally appeared on The Daily WTF.

Leave a Reply

Your email address will not be published.