Java 8 ifPresent vs ternary operator

2019-07-10 09:32发布

What do you think is better (with arguments, of course):

Optional.ofNullable( userName )
      .ifPresent( nonNullUserName  -> header.setUser( createUser( nonNullUserName ) ) );

or

header.setUser( userName == null ? createUser( userName ) : null );

The method createUser creates xml element and the intent of the whole peace of code is to set it in a SOAP request depending on presence of userName.

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.

UPDATE: 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.

2条回答
一夜七次
2楼-- · 2019-07-10 10:12

Optional was designed to be used as a return type for methods that can't always return a value, like for example getFirstElementOfAList(), which can't return anything else if the list is empty: returning null would be less clear than returning an empty Optional, and could mean that the first element is null, and not that there is no first element.

The designers don't intend Optional to be used as a convenience construct to chain methods. See the presentation by Stuart Marks (who works on the JDK), and especially what he says at the 28th minute:

It's generally a bad idea to create an Optional for the specific purpose of chaining methods [...]

You're of course free to disrespect the intended design and the best practices, but your colleagues might also be free to hate you for that :-)

Note that the two snippets you posted don't do the same thing. The first one does nothing if the user name is null, whereas the second one sets the user to null (which might in turn throw an exception or overwrite the previous value)

I would just use (assuming what you want is the first version):

if (userName != null) { 
    header.setUser(createUser(userName)); 
}

which is extremely clear, doesn't abuse Optional, and doesn't create an Optional instance just to chain methods.

查看更多
Anthone
3楼-- · 2019-07-10 10:21

They are different things, one is an Object, another is an operator. you shouldn't comparing between them. the first approach can be simplified to this, which will be more readable & descriablable:

Optional.ofNullable(userName).map(this::createUser).ifPresent(header::setUser);

IF you really want to comparing between them, the only one different is you have been mentioned above. and if you process condition or result more complex in an operator that will result to the expression is too complex to read.

查看更多
登录 后发表回答