Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save kijanowski/5673d02fe05065a932119f6b62e78adc to your computer and use it in GitHub Desktop.
conditionalException
// instead of
public void legacyConditionalException(String nullableValue) {
if (nullableValue != null) {
exec.methodOne(nullableValue);
} else {
throw new RuntimeException("don't like nulls");
}
}
// do it the functional way
public void vavrConditionalException(String nullableValue) {
Option.of(nullableValue)
.peek(exec::methodOne)
.getOrElseThrow(() -> new RuntimeException("don't like nulls"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment