question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reconsider the usage of `Optional<T>` in our API

See original GitHub issue

The early API such as Scheme.tryParse() and Server.activePort() returns an Optional while more recent APIs just return null.

Given that Optional tends to make user code verbose and JDK will nor make Optional a value type due to backward compatibility (which is a shame…), I think it’s better just returning a nullable value.

This guarantees breaking changes, but migration should be as simple as wrapping with Optional.ofNullable(), so … 😅

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
minwooxcommented, Jan 2, 2020

Let’s get rid of all of’em!

1reaction
trustincommented, Nov 30, 2020

Hi @alex-lx! Value types are ‘inline classes’:

Optional is not an inline class and I guess it will never be so due to backward compatibility reasons.

A user who prefers method chaining with Optional could wrap the return value with Optional.ofNullable(...), although it’s verbose. I wish Java introduces operators like ?, ?. ?: once Project Valhalla is ready.

Read more comments on GitHub >

github_iconTop Results From Across the Web

12 recipes for using the Optional class as it's meant to be used
Optional is a container that may hold a value, and it is useless to initialize it with null . API note: The empty()...
Read more >
Using Optional and Nullable Properties in API Requests
Learn how optional and nullable properties can be used flexibly in combinations in each parameter of your API requests made from a SDK....
Read more >
Uses for Optional - java - Stack Overflow
The main design goal of Optional is to provide a means for a function returning a value to indicate the absence of a...
Read more >
Best practices for API error handling and troubleshooting
This document provides details about error cases to be managed by your application, and the error codes and explanations you shall refer to...
Read more >
RESTful web API design - Best Practices - Microsoft Learn
All functionality should be discoverable so that client applications can fully use it. This guidance describes issues that you should consider ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found