Anti-Pattern: Optional as Method Parameters
You'll learn why passing Optional as a method parameter is considered poor API design and adds unnecessary complexity.
The Temptation
When you've embraced Optional for return types, it's tempting to accept Optional parameters too. After all, wouldn't processUser(Optional<User> user) clearly signal that the user might be absent?
Why This Is an Anti-Pattern
It forces callers to do extra work. Every caller must now wrap their value in an Optional before calling your method:
// Anti-pattern: Optional parameter
public void processUser(Optional<User> user) {
user.ifPresent(u -> System.out.println(u.getName()));
}
// Caller burden
processUser(Optional.of(currentUser)); // Has user
processUser(Optional.empty()); // No user
processUser(Optional.ofNullable(maybeUser)); // Nullable user
It creates two ways to express absence. Callers can pass Optional.empty() OR pass null itself. Now you need to handle both:
processUser(null); // What happens here? NullPointerException?
The Better Approach
Use method overloading or nullable parameters instead:
// Better: overloading
public void processUser(User user) {
if (user != null) {
System.out.println(user.getName());
}
}
public void processUser() {
// Handle absence case
}
The Optional wrapping becomes the caller's problem before calling your method, not something they must do to call it.
The Design Principle
Optional is designed as a return type to signal "this might be absent—handle it deliberately." As a parameter, it just adds wrapping ceremony without improving clarity.
Key Takeaway: Never use Optional as a method parameter—it complicates the API, creates ambiguity (null vs empty), and forces callers to wrap values unnecessarily. Use overloading or simple null-checks instead.