question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Return Optional instead of null

See original GitHub issue

Return Optional instead of null everywhere, eg. in getValue() calls.

Enable this

String msg = textInput.getValue().map(value -> "Hello " + value + "!").orElseGet("Don't be shy");
greeting.setText(msg);

instead of forcing our users to do this:

String value = textInput.getValue();
if (value == null || value.isEmpty()) {
    greeting.setText("Don't be shy");
} else {
    greeting.setText("Hello " + value + "!");
}

Null checks are a code smell in Java 8, and forcing if-else checking opens up the door for bugs.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:22 (22 by maintainers)

github_iconTop GitHub Comments

1reaction
mvysnycommented, Jan 11, 2018

For example Component.getId() returns Optional while Component.setId() takes String. This duality breaks the Java Bean spec and also borks Kotlin’s property mechanism and hence Kotlin thinks there is just a read-only property id of type Optional<String> - it ignores the setter since it is of different type.

I believe Optional is a misuse in this case since according to https://blog.jetbrains.com/idea/2017/08/code-smells-null/ this is

  1. Value was never initialised (whether accidentally or on purpose)

In this case the use of Optional is discouraged.

0reactions
plekucommented, Nov 22, 2019

I’m going to merge this issue to #3801 in case we ever get to improve the APIs or add features for better reactive/functional programming support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optional vs. null. What is the purpose of Optional in Java 8?
In Java 8 you can return an Optional instead of a null . Java 8 documentation says that an Optional is "A container...
Read more >
Null Check vs Optional? Are they same? - Medium
The main point of Optional is to indicate the absence of a value (i.e null value ) while returning value for the function....
Read more >
Why is using an optional preferential to null-checking the ...
Optional is used with the convention that methods with return type Optional never return null and usually also with the (less strong) convention...
Read more >
Chapter 10. Using Optional as a better alternative to null
When a value is present, the Optional class just wraps it. Conversely, the absence of a value is modeled with an “empty” optional...
Read more >
Guide To Java 8 Optional | Baeldung
This method returns true if the wrapped value is not null. ... However, instead of taking a value to return if the Optional...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found