title: Should nullable lambda arguments be replaced with transparent implementations?
body: <p>Say that a method has an optional <code>Action</code> or <code>Func&lt;T, T&gt;</code> argument that will usually be the default value of <code>null</code>.  This usually-<code>null</code> lambda expression will be called many times.</p>
  
  <p>In this situation, is it better to:</p>
  
  <ol>
  <li><p>Null-check the lambda expression before each use, or...</p>
  
  <p>a.  Call nullable <code>Action</code>'s using <code>action?.Invoke()</code>.</p>
  
  <p>b.  Call nullable transform <code>Func&lt;T, T&gt;</code>'s as <code>transform == null ? value : transform(value)</code>.</p></li>
  <li><p>...null-check the expression once when the method is called, then replace it with a transparent variant?</p>
  
  <p>a.  Replace <code>null</code> <code>Action</code>'s with <code>new Action(() =&gt; { })</code>.</p>
  
  <p>b.  Replace <code>null</code> <code>Func&lt;T, T&gt;</code>'s with <code>new Func&lt;T, T&gt;((T value) =&gt; { return value; })</code>.</p></li>
  </ol>
  
  <p>For this question, please assume:</p>
  
  <ul>
  <li><p>This is deeply nested math code, so its performance is relevant.</p></li>
  <li><p>The code's readability is not meaningfully affected.</p></li>
  </ul>
  
  <p>I ask this question because I frequently have methods that allow for optional lambda expressions, which are incredibly useful, but I'm never sure if I should <code>null</code>-check them on declaration or if I should just <code>null</code>-check them on each potential usage.</p>
  
  <p>I imagine that, if the lambda expression will only be executed once, then it's best to just <code>null</code>-check at that point.</p>
  
  <p>But, I'm curious about when the method constructs an object that will call the lambda expression many times.  Assuming that the argument will <em>usually</em> be <code>null</code>, then it seems like this question reduces to a simpler question: is it faster to <code>null</code>-check a lambda or execute one that doesn't do anything?</p>