Be careful Of This Java Optional Method

jarjanazy

Abdulcelil Cercenazi

Posted on October 13, 2021

Be careful Of This Java Optional Method

Let's Remember Java Optional 🤓

According to Oracle it's "A container object which may or may not contain a non-null value."
Optional was introduced in Java 8 and has been used by the SpringBoot team in many projects.


The most common usage of Optionals is in the Spring Data project. Let's look at the JpaRepository interface and an example method.
Say we have a User entity with an Id type of integer and that we have a JpaRepository for it

@Repository  
public interface IUserRepo extends JpaRepository<User, Integer>  
{  
    Optional<User> findByUserName(String userName);  
}
Enter fullscreen mode Exit fullscreen mode

We defined a method that searches for a user via their user name and returns an Optional of a User.


Optional's Convenience Methods 🙌

Optional comes in with many method meant to enable us to write clean and readable code.

  • map(..).or(...)
  • map(...).orElse(...)
  • check out Oracle's docs for the full list.

However, there is one method with a dangerously unexpected behavior


Meet The orElse Method 👀

According to Oracle's doc:

public T orElse(T other) 

Return the value if present, otherwise return other.

Now, we can add a method call as the parameter of the orElse, which will be run if the Optional is empty, right?

Yes, that's correct, BUT, what if I tell you that it will run anyways regardless of the presence of the value in Optional or not.


Let's test it ✍️

@Test  
public void orElseTest()  
{  
    String result = Optional.of("hello").orElse(someMethod());  
    assertThat(result).isEqualTo("hello");  
}  
private String someMethod()  
{  
    System.out.println("I am running !!");  
    return "hola";  
}
Enter fullscreen mode Exit fullscreen mode

The test does pass, but we notice that on the console we have the string "I am running" printed out.


Why is that? 🤨

  • Java runs the method to provide a value to be returned in the Else case.

So Be Careful ⛔️

We want to be careful if the method inside the orElse might have a side effect, because it will be run anyways.


What To Do then?

You can use the OrElseGet method which takes a supplier method to be executed if the Optional exists

Check out this wonderful comment for more details.


👉🏾 Also check out How to escape NullPointerExceptions in Java using Optional

💖 💪 🙅 🚩
jarjanazy
Abdulcelil Cercenazi

Posted on October 13, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related