Validate the arguments provided to all methods. This is the seventh in a series of posts on Code Gardening - the practice of making small improvements as you see them while you are working.
Everyone knows that good diagnostics make it easier to troubleshoot issues - validating method arguments at the start of a method gives you an opportunity to detect issues earlier, and to give better information.
If you’ve ever needed to track down the cause of a NullReferenceException that’s occurred deep in the bowels of an unfamiliar codebase, you’ll appreciate the importance of these checks.
Consider this method, from when we discussed using full words, not contractions:
Passing a null or empty string when searching for a TimeSeries isn’t going to work properly. Depending on the implementation of the repository, the call to
FindByClassification might throw an exception or (worse) just return a random result.
To catch this as early as possible, we can add a check to the start of the method to ensure the parameter we’ve passed is acceptable:
Now as soon as an inappropriate value for classification is passed to the routine an exception will be thrown, flagging the error sooner. With any luck, this will be closer to the original cause of the problem and will make it easier for someone to solve the underlying issue.
I’ve heard some protest that instrumenting every method in this manner will have a negative impact on performance.
While there is a theoretical performance degradation, these checks are extremely fast to do. In all the performance issues I’ve investigated in my career, none was caused by overly fastidious argument checking of this nature.
As always when it comes to performance issues, don’t guess, measure, then change.
Here’s another example, revisiting the
HasCode() method we wrote when discussing helper methods:
Here you can see the use of a simple extension method to validate the parameter easily and succinctly in a single line of code.
The definition of
Similar methods, like
MustNotBeEmpty() are left as an exercise for the reader.
If you like this technique and want to take it to the next level, check out the Code Contracts support in Visual Studio. There are similar tools available for many modern development stacks.