Skip to content

Instantly share code, notes, and snippets.

@LuizGC
Last active April 28, 2020 19:08
Show Gist options
  • Save LuizGC/e0572519e07f6b5c02489bd1dca12dfc to your computer and use it in GitHub Desktop.
Save LuizGC/e0572519e07f6b5c02489bd1dca12dfc to your computer and use it in GitHub Desktop.
//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