Last active
April 28, 2020 19:08
-
-
Save LuizGC/e0572519e07f6b5c02489bd1dca12dfc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Null pointer exception risk | |
item.getOne().getTwo().whereIsTheData(); | |
//or ugly | |
One one = item.getOne(); | |
if (one != null) { | |
Two two = one.getTwo(); | |
if (two != null) { | |
Data data = two.whereIsTheData(); | |
if (data == null) { | |
throw new NullPointerException(); | |
} | |
} | |
} | |
//or works only >=Java8 and still ugly | |
Optional.ofNullable(item) | |
.map(Item::getOne) | |
.map(One::getTwo) | |
.map(Two::whereIsTheData) | |
.orElseThrow() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment