Bad data happens. Sometimes it happens accidentally. Sometimes it happens intentionally. There’s one thing that is for certain though – it will happen.
This week we got hit by a particularly nasty bug. It happened entirely because the way a particular piece of code was running – it made the assumption that an incoming piece of data was ordered by a given attribute. In 99%+ of the cases, it was ordered, but no where in the specifications did it say that it would be ordered. In the cases where data came in out of order, we were doing things incorrectly, and we generated bad results.
I find this particularly true when working with Python and JSON data: never trust the format of data (especially the contents of Python dictionaries or JSON data). For example, in python, you would do something like:
someVal = someDictionary["level1"]["level2"]
If someDictionary was loaded from a piece of JSON from a web request, how do you know that “level1” is there? You don’t. In such a case, the above line will explode.
I am quite fond of the new C# way of handling things with the null conditional operator:
var someVal = someObject?.someProperty1?.someProperty2;
In the above code, someVal will only take on the value of someProperty2 if someObject is not null and someProperty1 is not null. One nice clean line. You still have to check the result of someVal, as it could still be null, but it is a nice, clean way of doing things.