DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Microservices With Apache Camel and Quarkus (Part 3)
  • DevOps vs. DevSecOps: The Debate
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  • What to Pay Attention to as Automation Upends the Developer Experience

Trending

  • Microservices With Apache Camel and Quarkus (Part 3)
  • DevOps vs. DevSecOps: The Debate
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  • What to Pay Attention to as Automation Upends the Developer Experience
  1. DZone
  2. Coding
  3. Java
  4. Idiomatic Optionals

Idiomatic Optionals

Developers still struggle to use Optionals properly in Java. Here is a simple rule to follow in your code so you don't fall into the trap.

Kamil Szymański user avatar by
Kamil Szymański
·
Mar. 28, 16 · Analysis
Like (7)
Save
Tweet
Share
7.35K Views

Join the DZone community and get the full member experience.

Join For Free

Java 8 was released over 20 months ago and Java 7 already reached the end of life, yet it’s not that hard to stumble upon new code that uses classes from Java 8 but looks like it was coded in a Java 7.The anti-pattern I’m talking about is most common when conditional logic needs to be applied to java.util.Optional and it looks like this:

public Optional<String> getClientNumber(String personalId) {
    Optional<Client> client = clientRepository.getByPersonalId(personalId);
    return client.isPresent() ? Optional.of(client.get().getNumber()) : Optional.empty();
}

while it should look like that:

public Optional<String> getClientNumber(String personalId) {
    return clientRepository.getByPersonalId(personalId)
            .map(Client::getNumber);
}

As a rule of thumb every time you use an if-else block or ternary operator to consume Optional’s value or produce an alternative one take a moment and check if it can be written in an idiomatic way.Most of the time it can and should be written in such way because it makes the code more readable.

The only case I came across that requires ternary operator is when you need to convert an Optional<T> to a Stream<T>, fortunately, that will be taken care of in Java 9.      

Java (programming language) Anti-pattern Operator (extension) Moment Blocks Convert (command) IT

Published at DZone with permission of Kamil Szymański, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Microservices With Apache Camel and Quarkus (Part 3)
  • DevOps vs. DevSecOps: The Debate
  • Decoding eBPF Observability: How eBPF Transforms Observability as We Know It
  • What to Pay Attention to as Automation Upends the Developer Experience

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: