title: How much null checking is enough?
body: <p>What are some guidelines for when it is <strong>not</strong> necessary to check for a null?</p>
  
  <p>A lot of the inherited code I've been working on as of late has null-checks ad nauseam. Null checks on trivial functions, null checks on API calls that state non-null returns, etc. In some cases, the null-checks are reasonable, but in many places a null is not a reasonable expectation.</p>
  
  <p>I've heard a number of arguments ranging from "You can't trust other code" to "ALWAYS program defensively" to "Until the language guarantees me a non-null value, I'm always gonna check." I certainly agree with many of those principles up to a point, but I've found excessive null-checking causes other problems that usually violate those tenets. Is the tenacious null checking really worth it?</p>
  
  <p>Frequently, I've observed codes with excess null checking to actually be of poorer quality, not of higher quality. Much of the code seems to be so focused on null-checks that the developer has lost sight of other important qualities, such as readability, correctness, or exception handling. In particular, I see a lot of code ignore the std::bad_alloc exception, but do a null-check on a <code>new</code>.</p>
  
  <p>In C++, I understand this to some extent due to the unpredictable behavior of dereferencing a null pointer; null dereference is handled more gracefully in Java, C#, Python, etc. Have I just seen poor-examples of vigilant null-checking or is there really something to this?</p>
  
  <p>This question is intended to be language agnostic, though I am mainly interested in C++, Java, and C#.</p>
  
  <hr>
  
  <p>Some examples of null-checking that I've seen that seem to be <em>excessive</em> include the following:</p>
  
  <hr>
  
  <p>This example seems to be accounting for non-standard compilers as C++ spec says a failed new throws an exception. Unless you are explicitly supporting non-compliant compilers, does this make sense? Does this make <em>any</em> sense in a managed language like Java or C# (or even C++/CLR)?</p>
  
  <pre><code>try {
     MyObject* obj = new MyObject(); 
     if(obj!=NULL) {
        //do something
     } else {
        //??? most code I see has log-it and move on
        //or it repeats what's in the exception handler
     }
  } catch(std::bad_alloc) {
     //Do something? normally--this code is wrong as it allocates
     //more memory and will likely fail, such as writing to a log file.
  }
  </code></pre>
  
  <hr>
  
  <p>Another example is when working on internal code. Particularly, if it's a small team who can define their own development practices, this seems unnecessary. On some projects or legacy code, trusting documentation may not be reasonable... but for new code that you or your team controls, is this really necessary?</p>
  
  <p>If a method, which you can see and can update (or can yell at the developer who is responsible) has a contract, is it still necessary to check for nulls?</p>
  
  <pre><code>//X is non-negative.
  //Returns an object or throws exception.
  MyObject* create(int x) {
     if(x&lt;0) throw;
     return new MyObject();
  }
  
  try {
     MyObject* x = create(unknownVar);
     if(x!=null) {
        //is this null check really necessary?
     }
  } catch {
     //do something
  }
  </code></pre>
  
  <hr>
  
  <p>When developing a private or otherwise internal function, is it really necessary to explicitly handle a null when the contract calls for non-null values only? Why would a null-check be preferable to an assert?</p>
  
  <p>(obviously, on your public API, null-checks are vital as it's considered impolite to yell at your users for incorrectly using the API)</p>
  
  <pre><code>//Internal use only--non-public, not part of public API
  //input must be non-null.
  //returns non-negative value, or -1 if failed
  int ParseType(String input) {
     if(input==null) return -1;
     //do something magic
     return value;
  }
  </code></pre>
  
  <p>Compared to:</p>
  
  <pre><code>//Internal use only--non-public, not part of public API
  //input must be non-null.
  //returns non-negative value
  int ParseType(String input) {
     assert(input!=null : "Input must be non-null.");
     //do something magic
     return value;
  }
  </code></pre>