How do I enforce null checking?

2019-01-18 04:11发布

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?

12条回答
看我几分像从前
2楼-- · 2019-01-18 04:57

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.

查看更多
萌系小妹纸
3楼-- · 2019-01-18 04:57
Rolldiameter
4楼-- · 2019-01-18 05:04

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:

  • That those lines of code are all the lines of code that should've been there
  • That those lines of code are operating correctly (are you testing all edge cases?)

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).

查看更多
Deceive 欺骗
5楼-- · 2019-01-18 05:06

The .NET framework was looking to enforce compile time null reference checks by using an ! modifier.

public void MyMethod(!string cannotBeNull)

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:

public class ExternalFacing
{
  public void MyMethod(string arg)
  {
     if (String.IsNullOrEmpty(arg))
        throw new ArgumentNullException(arg);

     implementationDependency.DoSomething(arg);
   }
}

internal class InternalClass
{
    public void DoSomething(string arg)
    {
         // shouldn't have to enforce null here.
    }
}

Then apply the appropriate unit tests to the External class to expect ArgumentNullExceptions.

查看更多
贪生不怕死
6楼-- · 2019-01-18 05:11

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.

查看更多
做自己的国王
7楼-- · 2019-01-18 05:11

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.

查看更多
登录 后发表回答