title: Parameter Validation Best Practices
body: <p>Imagine you have an application which is some kind of <em>front-end</em> to all your business logic. This front-end has a lot of DLLs upon which it depends, and the methods in those DLLs may call each other repeatedly upon a single execution of a given method in the front-end. If the users of your application do not directly access those DLLs, should you...</p>
  
  <p>1) Risk a (small) performance hit and validate parameters in each of those methods, even if you can end up validating the same parameters some 5 times; or</p>
  
  <p>2) Risk unexpected behaviour and assume that, as you validate input parameters, all the other possible parameters passed to and from your internal code are valid (for example, neither null nor empty)?</p>
  
  <p><strong>Edit:</strong> Just to give an example, suppose you have a Regex <code>RegexA</code> and a method</p>
  
  <pre><code>internal bool Matches(string expression)
  {
      return RegexA.IsMatch(expression);
  }
  </code></pre>
  
  <p><code>IsMatch</code> will throw an exception on a null parameter, but not on the empty string. If you know  beforehand that an empty string will never be a match to that Regex, should  you use <code>if (String.IsNullOrEmpty(expression))</code> before, even knowing that it may be validated for nullity inside the <code>IsMatch</code> framework method? In this case you are clearly repeating a validation, but is it better to repeat it or to risk?</p>