title: Java 8 ifPresent vs ternary operator
body: <p>What do you think is better (with arguments, of course):</p>
  
  <pre><code>Optional.ofNullable( userName )
        .ifPresent( nonNullUserName  -&gt; header.setUser( createUser( nonNullUserName ) ) );
  </code></pre>
  
  <p>or</p>
  
  <pre><code>header.setUser( userName == null ? createUser( userName ) : null );
  </code></pre>
  
  <p>The method <code>createUser</code> creates xml element and the intent of the whole peace of code is to set it in a SOAP request depending on presence of <code>userName</code>. </p>
  
  <p>The benefits of the first approach I see is the absence of useless operations, the code does one thing and no more. But the second approach lets you save one more line of code thus appearing to be more laconic.</p>
  
  <p><strong>UPDATE:</strong> I guess I missed a thing I actually implied and it caused certain misunderstanding. It would be better to provide cleaner examples with explanations if you have some.</p>