![]() ![]() We can update our model to make use of Optional, as shown in Listing 2: You can view Optional as a single-value container that either contains a value or doesn't (it is then said to be "empty"), as illustrated in Figure 2. It is a class that encapsulates an optional value, as illustrated in Listing 2 below and in Figure 1. Java SE 8 introduces a new class called j that is inspired from the ideas of Haskell and Scala. You might now wonder, "so, what about Java SE 8?" Optional in a Nutshell OK, we diverged a bit and all this sounds fairly abstract. You then have to explicitly check whether a value is present or not using operations available on the Option type, which enforces the idea of "null checking." You can no longer "forget to do it" because it is enforced by the type system. Scala has a similar construct called Option to encapsulate the presence or absence of a value of type T. A value of type Maybe can contain either a value of a given type or nothing. Haskell includes a Maybe type, which essentially encapsulates an optional value. Other functional languages, such as Haskell and Scala, take a different view. In the following, if the expression that uses the safe navigation operator returns null, the default value "UNKNOWN" is returned otherwise, the available version tag is returned.Ĭomputer?.getSoundcard()?.getUSB()?.getVersion() ?: "UNKNOWN" In addition, Groovy also includes the Elvis operator " ?:" (if you look at it sideways, you'll recognize Elvis' famous hair), which can be used for simple cases when a default value is needed. You don't need to write complex nested conditions to check for null. In this case, the variable version will be assigned to null if computer is null, or getSoundcard() returns null, or getUSB() returns null. String version = computer?.getSoundcard()?.getUSB()?.getVersion() (Note that it is soon to be included in C#, too, and it was proposed for Java SE 7 but didn't make it into that release.) It works as follows: Languages such as Groovy have a safe navigation operator represented by " ?." to safely navigate through potential null references. To give some context, let's briefly look at what other programming languages have to offer. What we need is a better way to model the absence and presence of a value. In fact, they are decreasing the overall readability of our program.įurthermore, it is an error-prone process what if you forget to check that one property could be null? I will argue in this article that using null to represent the absence of a value is a wrong approach. In addition, it's just annoying that these checks get in the way of the business logic. Unfortunately, we need a lot of boilerplate code to make sure we don't get a NullPointerException. However, you can see that the code in Listing 1 quickly becomes very ugly due to the nested checks. Soundcard soundcard = computer.getSoundcard() What can you do to prevent unintended null pointer exceptions? You can be defensive and add checks to prevent null dereferences, as shown in Listing 1: ![]() I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement." It was the invention of the null reference in 1965. To give some historical context, Tony Hoare-one of the giants of computer science-wrote, "I call it my billion-dollar mistake. Imagine if your program was running on a customer's machine what would your customer say if the program suddenly failed? ![]() Unfortunately, this means the call to getUSB() will try to return the USB port of a null reference, which will result in a NullPointerException at runtime and stop your program from running further. So what is the result of getSoundcard()?Ī common (bad) practice is to return the null reference to indicate the absence of a sound card. However, many computers (for example, the Raspberry Pi) don't actually ship with a sound card. String version = computer.getSoundcard().getUSB().getVersion() What's possibly problematic with the following code? Let's consider a nested object structure for a Computer, as illustrated in Figure 1.įigure 1: A nested structure for representing a Computer Let's start with an example to see the dangers of null. Java SE 8 introduces a new class called that can alleviate some of these problems. Joking aside, the null reference is the source of many problems because it is often used to denote the absence of a value. Make your code more readable and protect it against null pointer exceptions.Ī wise man once said you are not a real Java programmer until you've dealt with a null pointer exception. ![]()
0 Comments
Leave a Reply. |