title: Should you assert not null with the assert statement in production code?
body: <p>I've seen <a href="https://stackoverflow.com/questions/4537609/is-it-good-practice-to-use-assert-in-java">this</a> question but have a few more questions about the usage of the <code>assert</code> keyword. I was debating with a few other coders about using <code>assert</code>. For this use case, there was a method that can return null if certain prerequisites are met. The code I wrote calls the method, then asserts it doesn't return null, and continues to use the returned object.</p>
  
  <p>Example:</p>
  
  <pre><code>class CustomObject {
      private Object object;
  
      @Nullable
      public Object getObject() {
          return (object == null) ? generateObject() : object;
      }
  }
  </code></pre>
  
  <p>Now imagine I use it like this:</p>
  
  <pre><code>public void useObject(CustomObject customObject) {
      object = customObject.getObject();
      assert object != null;
      // Do stuff using object, which would throw a NPE if object is null.
  }
  </code></pre>
  
  <p>I was told I should remove the <code>assert</code>, that they should <strong>never</strong> be used in production code, only be used in testing. Is that true?</p>