The other day I found myself debugging the dreaded NullReferenceException “Object Reference not set to an instance of an object” on a piece of code written by a fairly Senior Developer. If you’ve been developing on .NET for more than 5 minutes then I’m sure you have come across this in your daily work and it is incredibly frustrating to try and debug. With this post I hope to demonstrate how by using Defensive Programming you can make your API’s more robust and as a result reduce the number of bugs in your code. If you’re a bit hazy on Defensive Programming and it’s cousins then I suggest you read the following: Defensive Programming Fail Fast Design by Contract (DbC) Validating method arguments for public methods should be a very simple concept to grasp and for most skilled developers is a as natural as writing If statements. The guiding principle I follow is to trust no single input and ensure that code only executes when the supplied arg...