Skip to content

Instantly share code, notes, and snippets.

@kijanowski
Last active November 4, 2018 19:14
Show Gist options
  • Select an option

  • Save kijanowski/afcbf26c64aaf362aa6e669700a50ee5 to your computer and use it in GitHub Desktop.

Select an option

Save kijanowski/afcbf26c64aaf362aa6e669700a50ee5 to your computer and use it in GitHub Desktop.
conditionalExecutionOfDifferentMethods
// instead of
public void legacyConditionalExecutionOfDifferentMethods(String nullableValue) {
if (nullableValue == null) {
exec.methodOne(DEFAULT);
} else {
exec.methodTwo(nullableValue);
}
}
// do it the functional way
public void vavrConditionalExecutionOfDifferentMethods(String nullableValue) {
Option.of(nullableValue)
.peek(exec::methodTwo)
.onEmpty(() -> exec.methodOne(DEFAULT));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment