title: Should you check parameters passed into function before passing them, or check them in the function?
body: <p>As a good practice, do you think one should verify passed parameters within a function to which the parameters are being passed, or simply make sure the function will always accept correct parameters?</p>
  
  <p>Consider the following code:</p>
  
  <pre><code>Matrix * add_matrices(const Matrix * left, const Matrix * right)
  {
      assert(left-&gt;rowsCount == right-&gt;rowsCount
          &amp;&amp; left-&gt;colsCount == right-&gt;colsCount);
  
      int rowsCount = left-&gt;rowsCount;
      int colsCount = left-&gt;colsCount;
  
      Matrix * matOut = create_matrix(rowsCount, colsCount);
  
      int i = 0;
      int j = 0;
      for (i; i &lt; rowsCount; ++i)
      {
          for (j; j &lt; colsCount; ++j)
          {
              matOut-&gt;matrix[i][j] = left-&gt;matrix[i][j] + right-&gt;matrix[i][j];
          }
      }
  
      return matOut;
  }
  </code></pre>
  
  <p>Do you think I should check the parameters before passing them to the function or after, ie. in the function? What is a better practice or is it programmer dependant?</p>