Quite often, we have to test if a field or variable is null
or has a value.
Conditional checks in traditional java relied on an if condition.
Failure to check and assumption of existence of a value can lead to the
ever-so-popular NullPointerException
.
Sir Charles Anthony Richard Hoare (Tony Hoare), creator of the null reference rued the creation of null in 2009:
>I call it my billion-dollar mistake. It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object-oriented language (ALGOL W). My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But, I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.
Optional was added in Java 8 as a possible solution to errant null issues. An Optional wrapper
over a field or variable ensures there is a check - or - a deliberate attempt to get a value from
the wrapper. The kata will help us better understand the features of java.util.Optional
A code-kata is a coding exercise that builds muscle memory by a practice of programming to arrive at a known solution.
The essence of the exercise (presentation material and code kata) is to demonstrate the usage patterns for the java API or functionality.
This set of code katas rely on fixing broken tests. The tests may have multiple solutions, the intent is to learn and experiment.
The project contains several JUnit tests that fail.
-
Run the test class(es).
-
One or more tests will fail with the test failure message.
-
Fix the failing tests by using
HINT
andTODO
comments. -
Repeat above steps until all tests pass.
-
Check the solutions to see if there are other ways to solve. (Remember, the solution may be less performant/optimal than yours)
-
Rinse and repeat (delete and checkout again, then back to Step 1) to build muscle memory.
How to prepare for coding along
This kata is developed as a Java maven project.Ensure that you have:
-
Apache Maven 3.6.x or above. Tested with Apache Maven 3.6.0. Link: https://maven.apache.org/download.cgi
-
JDK 11 or above. Tested with OpenJDK 11 Link: http://jdk.java.net/11/
-
Your favorite Java IDE. IntelliJ IDEA Ultimate was used to develop this kata.
The structure of the project:
|____pom.xml
|____README.md
|
|____src
| |
| |____test <------------------- Kata Tests
| | |____java
| | |____none
| | |____cvg
| | |____optional
| |
| |____solutions <------------------- Solutions
| | |____java
| | |____none
| | |____cvg
| | |____optional
The JUnit tests listed below are set to utilize the Java Optional API features.
- TestKata1OptionalCreationAndFetchingValues.java
-
show creation of and fetching value from an Optional.
- TestKata2OptionalConditionalFetching.java
-
show conditional checks and alternate actions when fetching values from an Optional.
- TestKata3StreamsAndOptionals.java
-
show the usage of Optional in Java Stream API.
Kata Test | Solution |
---|---|