Usually, yes — returning `Optional<T>` is better than returning `null` when “no result” is a normal, expected outcome. Why: - `Optional` makes the absence explicit in the method signature. - It reduces accidental `NullPointerException`s. - It forces callers to handle the empty case intentionally. Example: ```java public Optional<User> findUserById(String id) { return repository.lookup(id); } ``` Then the caller can do: ```java findUserById(id).ifPresent(this::sendEmail); ``` or ```java User u...
Scanned 9/5/2026
Install to Claude Code
npx -y skills add HoangNguyen0403/agent-skills-standard --skill java-best-practices --agent claude-codeInstalls into .claude/skills of the current project.
Are you the author of Java Best Practices?
Add the live security badge to your README — it updates automatically with every re-scan.
[](https://www.skillsdirectory.com/skills/hoangnguyen0403-java-best-practices-b129971e)More formats (shields.io, HTML) on the badges page.
Usually, yes — returning `Optional<T>` is better than returning `null` when “no result” is a normal, expected outcome.
Why:
- `Optional` makes the absence explicit in the method signature.
- It reduces accidental `NullPointerException`s.
- It forces callers to handle the empty case intentionally.
Example:
```java
public Optional<User> findUserById(String id) {
return repository.lookup(id);
}
```
Then the caller can do:
```java
findUserById(id).ifPresent(this::sendEmail);
```
or
```java
User user = findUserById(id).orElseThrow(() -> new UserNotFoundException(id));
```
A few caveats:
- Don’t use `Optional` for fields or most method parameters.
- For collections, return an empty collection instead of `Optional<List<T>>` in most cases.
- If the method is internal and performance-critical, `null` may still be acceptable, but it should be a deliberate choice.
So the general Java best practice is: don’t return `null` for “not found”; prefer `Optional<T>` or an empty collection, depending on the return type.
Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.
No comments yet. Be the first to comment!