How much null checking is enough?

后端 未结 18 955
清酒与你
清酒与你 2021-01-30 01:10

What are some guidelines for when it is not necessary to check for a null?

A lot of the inherited code I\'ve been working on as of late has null-checks

相关标签:
18条回答
  • 2021-01-30 01:36

    One thing to remember that your code that you write today while it may be a small team and you can have good documentation, will turn into legacy code that someone else will have to maintain. I use the following rules:

    1. If I'm writing a public API that will be exposed to others, then I will do null checks on all reference parameters.

    2. If I'm writing an internal component to my application, I write null checks when I need to do something special when a null exists, or when I want to make it very clear. Otherwise I don't mind getting the null reference exception since that is also fairly clear what is going on.

    3. When working with return data from other peoples frameworks, I only check for null when it is possible and valid to have a null returned. If their contract says it doesn't return nulls, I won't do the check.

    0 讨论(0)
  • 2021-01-30 01:36

    Part of this depends on how the code is used -- if it is a method available only within a project vs. a public API, for example. API error checking requires something stronger than an assertion.

    So while this is fine within a project where it's supported with unit tests and stuff like that:

    internal void DoThis(Something thing)
    {
        Debug.Assert(thing != null, "Arg [thing] cannot be null.");
        //...
    }
    

    in a method where you don't have control over who calls it, something like this may be better:

    public void DoThis(Something thing)
    {
        if (thing == null)
        {
            throw new ArgumentException("Arg [thing] cannot be null.");
        }
        //...
    }
    
    0 讨论(0)
  • 2021-01-30 01:37

    I don't think it's bad code. A fair amount of Windows/Linux API calls return NULL on failure of some sort. So, of course, I check for failure in the manner the API specifies. Usually I wind up passing control flow to an error module of some fashion instead of duplicating error-handling code.

    0 讨论(0)
  • 2021-01-30 01:38

    If you write the code and its contract, you are responsible for using it in terms of its contract and ensuring the contract is correct. If you say "returns a non-null" x, then the caller should not check for null. If a null pointer exception then occurs with that reference/pointer, it is your contract that is incorrect.

    Null checking should only go to the extreme when using a library that is untrusted, or does not have a proper contract. If it is your development team's code, stress that the contracts must not be broken, and track down the person who uses the contract incorrectly when bugs occur.

    0 讨论(0)
  • 2021-01-30 01:38

    Personally I think null testing is unnnecessary in the great majority of cases. If new fails or malloc fails you have bigger issues and the chance of recovering is just about nil in cases where you're not writing a memory checker! Also null testing hides bugs a lot in the development phases since the "null" clauses are frequently just empty and do nothing.

    0 讨论(0)
  • 2021-01-30 01:38

    Lower level code should check use from higher level code. Usually this means checking arguments, but it can mean checking return values from upcalls. Upcall arguments need not be checked.

    The aim is to catch bugs in immediate and obvious ways, as well as documenting the contract in code that does not lie.

    0 讨论(0)
提交回复
热议问题