Skip to content

Instantly share code, notes, and snippets.

@kijanowski
kijanowski / nestedConditionsWithExecution.java
Last active November 4, 2018 19:16
nestedConditionsWithExecution
// instead of
public void legacyNestedConditionsWithExecution(String nullableValue) {
if (nullableValue == null) {
exec.methodOne(DEFAULT);
} else if (nullableValue.startsWith("ONE")) {
exec.methodOne(nullableValue);
} else if (nullableValue.startsWith("TWO")) {
exec.methodTwo(nullableValue);
} else {
exec.methodThree(nullableValue);
@kijanowski
kijanowski / nestedConditionsWithReturn.java
Last active November 4, 2018 19:16
nestedConditionsWithReturn
// instead of
public String legacyNestedConditionsWithReturn(String nullableValue) {
if (nullableValue == null) {
return DEFAULT;
} else if (nullableValue.startsWith("ONE")) {
return nullableValue.concat("123");
} else if (nullableValue.startsWith("TWO")) {
return nullableValue.concat("456");
} else {
return nullableValue.concat("789");
@kijanowski
kijanowski / nestedConditionWithMultipleValues.java
Last active November 4, 2018 19:16
nestedConditionWithMultipleValues
// instead of
public void legacyNestedConditionWithMultipleValues(String nullableValueA, String nullableValueB) {
if (nullableValueA == null && nullableValueB == null) {
exec.methodOne(DEFAULT);
} else if (nullableValueA == null) { // nullableValueB != null
exec.methodOne(nullableValueB);
} else if (nullableValueB == null) { // nullableValueA != null
exec.methodTwo(nullableValueA);
} else { // nullableValueA != null && nullableValueB != null
exec.methodThree(DEFAULT);
@kijanowski
kijanowski / complexAndConditional.java
Last active November 4, 2018 19:15
complexAndConditional
// instead of
public String legacyComplexAndConditional(String nullableValue) {
if (nullableValue != null && nullableValue.startsWith("ONE")) {
return "1st condition";
} else {
return DEFAULT;
}
}
// do it the functional way
@kijanowski
kijanowski / conditionalException.java
Last active November 4, 2018 19:15
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
@kijanowski
kijanowski / conditionalExecutionOfDifferentMethods.java
Last active November 4, 2018 19:14
conditionalExecutionOfDifferentMethods
// instead of
public void legacyConditionalExecutionOfDifferentMethods(String nullableValue) {
if (nullableValue == null) {
exec.methodOne(DEFAULT);
} else {
exec.methodTwo(nullableValue);
}
}
// do it the functional way
@kijanowski
kijanowski / conditionalExecutionOfTheSameMethod.java
Last active November 4, 2018 19:14
conditionalExecutionOfTheSameMethod
// instead of
public void legacyConditionalExecutionOfTheSameMethod(String nullableValue) {
if (nullableValue == null) {
exec.methodOne(DEFAULT);
} else {
exec.methodOne(nullableValue);
}
}
// do it the functional way
@kijanowski
kijanowski / nullConditionalExecution.java
Last active November 4, 2018 19:14
nullConditionalExecution
// instead of
public void legacyNullConditionalExecution(String nullableValue) {
if (nullableValue != null) {
exec.methodOne(nullableValue);
}
}
// do it the functional way
public void vavrNullConditionalExecution(String nullableValue) {
Option.of(nullableValue)
@kijanowski
kijanowski / legacyNullCheck.java
Created November 1, 2018 18:56
legacyNullCheck
// instead of
public String legacyNullCheck(String nullableValue) {
if (nullableValue != null) {
return nullableValue.toLowerCase();
}
return nullableValue;
}
// do it the functional way
public String vavrNullCheck(String nullableValue) {
// instead of
private static final java.util.Map<Integer, String> myMap = new java.util.HashMap<>();
static {
myMap.put(1, "one");
myMap.put(2, "two");
}
// or
private static final java.util.Map<Integer, String> myJava9Map = java.util.Map.of(
1, "one",