Back to all reviewers

Prefer Optional over nulls

quarkusio/quarkus
Based on 5 comments
Java

Use Java's Optional API instead of null checks to improve code readability, safety, and maintainability. When dealing with potentially absent values:

Null Handling Java

Reviewer Prompt

Use Java’s Optional API instead of null checks to improve code readability, safety, and maintainability. When dealing with potentially absent values:

  1. Return Optional instead of T that might be null
  2. Use Optional’s functional methods (ifPresent, map, flatMap, orElse) rather than null checks
  3. Avoid Optional.get() without first checking isPresent()
  4. When comparing values that might be null, use Objects.equals() or place potentially null values on the right side of equals comparisons

Example:

// Not recommended
String principalClaim = resolvedContext.oidcConfig().token().principalClaim().orElse(null);
if (principalClaim != null && !tokenJson.containsKey(principalClaim)) {
    // do something with principalClaim
}

// Recommended
resolvedContext.oidcConfig().token().principalClaim().ifPresent(claim -> {
    if (!tokenJson.containsKey(claim)) {
        // do something with claim
    }
});

For performance-critical code paths, consider using specialized patterns like returning pre-defined constants (e.g., Collections.emptyMap()) instead of allocating new objects when a value is absent.

5
Comments Analyzed
Java
Primary Language
Null Handling
Category

Source Discussions