title: Best practices when validating function arguments for public/private methods
body: <p>Doing some research, it seems that people generally agree that arguments to public methods should be validated while private functions typically do not. This causes me to have some questions, but I have not been able to find a satisfactory answer so far.</p>
  
  <p>Example:</p>
  
  <pre><code>public void DoSomething(int i)
  {
      if (i &lt; 0)
          throw new ArgumentOutOfRangeException("i");
  
      double d = DoWork(i);
  }
  
  private double DoWork(int i)
  {
      double ret = ...; // some calculation
      return ret;
  }
  </code></pre>
  
  <p>Thoughts:</p>
  
  <ol>
  <li><p>What if the requirement of <code>i</code> to be non-negative changes inside <code>DoWork()</code>? The design risks leaving outdated validation checks in place. The programmer is responsible for adjusting the usage of a function that has changed, I know, but it leaves me wondering if there is a better way to minimize risk of error.</p></li>
  <li><p>What about different calls to <code>DoWork()</code> not from <code>DoSomething()</code>? Must we validate the arguments redundantly?</p></li>
  </ol>
  
  <p><b></b></p>
  
  <pre><code>public void DoSomething(int i)
  {
      if (i &lt; 0)
          throw new ArgumentOutOfRangeException("i");
  
      double d = DoWork(i);
  }
  
  public void DoSomethingElse()
  {
      int i = 5;
      if (i &lt; 0)
          throw new ArgumentOutOfRangeException("i");
  
      double d = DoWork(i);
  }
  
  private double DoWork(int i)
  {
      double ret = ...; // some calculation
      return ret;
  }
  </code></pre>
  
  <p>This can be cleaned up a little by putting the check into a function of its own. Then there is the risk that a new function making a call to <code>DoWork(int i)</code> will forget to validate <code>i</code>.</p>
  
  <pre><code>public void DoSomething(int i)
  {
      ThrowIfIntegerIsNegative(i);
  
      double d = DoWork(i);
  }
  
  public void DoSomethingElse()
  {
      int i = 5;
      ThrowIfIntegerIsNegative(i);
  
      double d = DoWork(i);
  }
  
  static void ThrowIfIntegerIsNegative(int i)
  {
      if (i &lt; 0)
          throw new ArgumentOutOfRangeException("i");
  }
  
  private double DoWork(int i)
  {
      double ret = ...; // some calculation
      return ret;
  }
  </code></pre>
  
  <p>Is that at all better than this?</p>
  
  <pre><code>public void DoSomething(int i)
  {
      double d = DoWork(i);
  }
  
  public void DoSomethingElse()
  {
      double d = DoWork(5);
  }
  
  private double DoWork(int i)
  {
      if (i &lt; 0)
          throw new ArgumentOutOfRangeException("i");
  
      double ret = ...; // some calculation
      return ret;
  }
  </code></pre>
  
  <p>Depending on the situation, these are some goals I am trying to achieve simultaneously:</p>
  
  <ol>
  <li>Have the argument validation in a single place (and possibly inside the function using the argument)</li>
  <li>Report an error sooner rather than later (might not want to have a bunch of code run for an hour just to fail at the end for some bad user input)</li>
  <li>Avoid validating arguments multiple times</li>
  <li>Avoid performance impact in release code</li>
  </ol>
  
  <p>How do you strike a balance? What methodology has worked best for you? I would greatly appreciate any insight.</p>