You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[java] New rules around Optional usage for Java 8+ #1034
@jsotuyod woudln't be better to have an issue for each rule? This way we could track the progress of each rule separately. i.e I'm starting to implement:
Never return null in a method returning Optional / OptionalInt / OptionalDouble / OptionalLong. Use the empty() optionals instead.
@andreybleme on this case, since the checks are very punctual, but all related to poor usage of the Optional APIs, so I'd rather have a single rule, and properties to enable / disable individual checks if it makes sense (ie: as we did with LinguisticNaming)
Nonetheless, it's perfectly fine to build that rule incrementally, adding a check at a time.
The usage of Optional.of(...) vs. Optional.ofNullable(...) is also tricky. The first will throw a NPE, while the second will return an empty optional, if null was provided.
Simpler, easily understandable rules such as not allowing an Optional
object to be set to null and disallowing the use of Optionals as member
fields, in constructor , setter and method arguments , in collections and
returning empty collections or arrays as Optionals could be the first ones
to be implemented.
That's my take on it.
The Optional.of versus OptionalofNullable via @adangel is simple enough to be implemented as well.
Activity
oowekyala commentedon Apr 22, 2018
Additionally, with java 9+,
can be replaced by
optional.ifPresentOrElse(lambda1, lambda2)andreybleme commentedon Oct 8, 2018
@jsotuyod May I start to implement some of these rules?
jsotuyod commentedon Oct 8, 2018
@andreybleme most definitively!
andreybleme commentedon Oct 8, 2018
@jsotuyod woudln't be better to have an issue for each rule? This way we could track the progress of each rule separately. i.e I'm starting to implement:
jsotuyod commentedon Oct 9, 2018
@andreybleme on this case, since the checks are very punctual, but all related to poor usage of the
OptionalAPIs, so I'd rather have a single rule, and properties to enable / disable individual checks if it makes sense (ie: as we did withLinguisticNaming)Nonetheless, it's perfectly fine to build that rule incrementally, adding a check at a time.
stefankatic commentedon Jul 27, 2019
Any progress on this?
jsotuyod commentedon Jul 30, 2019
@stefankatic none, we haven't heard back from @andreybleme. Contributions are always welcomed.
linusjf commentedon Oct 1, 2019
My bad. I ought to have checked issues to see if there were any existing write-ups.
I'll just post the article here too:
https://dzone.com/articles/using-optional-correctly-is-not-optional
linusjf commentedon Oct 1, 2019
It might be a good idea to revisit splitting the rule into multiple ones. That way, contributors can work independently on each piece.
adangel commentedon Oct 3, 2019
The usage of
Optional.of(...)vs.Optional.ofNullable(...)is also tricky. The first will throw a NPE, while the second will return an empty optional, if null was provided.linusjf commentedon Oct 3, 2019