This file contains hidden or 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
// 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); |
This file contains hidden or 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
// 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"); |
This file contains hidden or 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
// 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); |
This file contains hidden or 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
// instead of | |
public String legacyComplexAndConditional(String nullableValue) { | |
if (nullableValue != null && nullableValue.startsWith("ONE")) { | |
return "1st condition"; | |
} else { | |
return DEFAULT; | |
} | |
} | |
// do it the functional way |
This file contains hidden or 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
// 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 |
This file contains hidden or 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
// instead of | |
public void legacyConditionalExecutionOfDifferentMethods(String nullableValue) { | |
if (nullableValue == null) { | |
exec.methodOne(DEFAULT); | |
} else { | |
exec.methodTwo(nullableValue); | |
} | |
} | |
// do it the functional way |
This file contains hidden or 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
// instead of | |
public void legacyConditionalExecutionOfTheSameMethod(String nullableValue) { | |
if (nullableValue == null) { | |
exec.methodOne(DEFAULT); | |
} else { | |
exec.methodOne(nullableValue); | |
} | |
} | |
// do it the functional way |
This file contains hidden or 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
// 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) |
This file contains hidden or 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
// 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) { |
This file contains hidden or 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
// 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", |
OlderNewer