I'm working on a large project where, even with 10s of 1000s of automated tests and 100% code coverage, we're getting a ridiculous number of errors. About 95% of errors we get are NullReferenceExceptions.
Is there any way to enforce null-checking at compile time?
Barring that, is there any way to automagically enforce null-checking in unit tests without having to write the tests for null cases myself?
You cannot have null checking at compile time as at compile time the objects are only Types and only at runtime the Types are converted to instances that have a concrete value ... here null.
Maybe you should take a look at custom code analysis checkin policies for TFS
http://weblogs.asp.net/uruit/archive/2009/03/17/writing-custom-rules-for-tfs-2008-code-analysis-check-in-policy.aspx
100% code coverage means nothing.
It is a false sense of security.
The only thing you're measuring is that you're executing all the lines of code.
Not:
For instance, if your procedure to deal with a fire contains 1 step "run out of the building", then even if that happens in 100% of the cases, perhaps a better procedure would be to "alert the fire department, try to stop the fire, then run out if all else fails".
There is nothing built into C# that will help you with this without you specifically going in and adding code, either code contracts (.NET 4.0) or specific IF-statements (<4.0).
The .NET framework was looking to enforce compile time null reference checks by using an ! modifier.
But alas, we don't have compile time checking. Your best bet is to minimize the amount occurrences for external callers to pass null values and then enforce null checks on public facing methods:
Then apply the appropriate unit tests to the External class to expect ArgumentNullExceptions.
I may be wrong, but I think FxCop has a rule that suggests you add null reference checks to your code. You could try running your assemblies through the tool and see what it has to say.
Check out Gendarme, it can be run post-build alongside your tests (possibly before them, if you wish) and has a few rules relating to
null
checks. You can also fairly trivially write your own.