Programming

Optional in Java: When Better Code Is Not an Alternative

Spread the love


If you are a Java developer, you have handled issues with Java for a long time. The most often problem in the Java world is the NullPointerException.

Nullity is a natural consequence of reference types. If there is a reference, it has to refer to some object – or be null. If there is no this option, all the variable was initialized with some non-null expression – and even then, you’d have issues if variables were read during the initialization phase.

Once this issue happened several times and brought bugs, this issue became the Billion Dollar Mistake.

When we avoid this mistake, we have several patterns, such as Null values: In object-oriented computer programming, a null object is an object with no referenced value or defined neutral (“null”) behavior. The null object design pattern describes such objects uses and behavior (or lack thereof).

Even with the Null values pattern, for some cases, we need to check if the instance is null, and if it is not null, we can do some action on it.

Person otavio = database.findByName("Otavio);

if(otavio != null) {
  //do some action here
}

The exception to these rules is the collections where we always should return references such as an emptyList using the principles from the effective Java.

Since Java 8, Java Developers have one good way to avoid the classic NullPointerException. However, it is still a problem to use it optimally. This video will help you combine this API with your business. It includes an enrichment to apply your entities with your rich model.

We can see the use of this API in several frameworks, such as Spring Framework, which has Spring Data where it returns an Optional reference in the repository interfaces.

The goal is to return an Optional in each value that might be null such as the find by id method. This way, a user can operate with this alternative and check this prospect inside the Java API.

This video will explore the API available in the Java world since Java 8 from a regular programmer’s perspective and how we can use and explore this API more inside our code, not as a framework.

Are you ready to understand the pitfalls of this API and how to archive a clean and safe design in your code? So this video is for you. 

We’ll understand:

  • The performance impact both: memory and process.
  • Why premature performance is dangers
  • We’ll explain how to apply Optional with our entities to make creating and maintaining a rich model easier.
  • We have several data frameworks, such as Spring Data, but how to combine this with our current service with controller layers?
  • As with any trade-off, we also need to understand why we should not use the Optional API.
  • Much more!

Thus, after this video, we’ll learn the best way to apply the Optional API, how to combine it with old and new APIs, and mainly, to take advantage of this new API to avoid inconsistency such as the billion-dollar mistake: Null pointer exception.



Source link

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button