Skip to content

[java] New rules around Optional usage for Java 8+ #1034

Open

Description

@jsotuyod

I'm seeing some bad usages of Optional which I think we can translate into a bunch of new rules for Java.

  1. Never return null in a method returning Optional / OptionalInt / OptionalDouble / OptionalLong. Use the empty() optionals instead.
  2. Never null-check an optional, but use isPresent()
  3. if (optional.isPresent()) { single_line } should be optional.ifPresent(lambda)
  4. optional.isPresent() ? Optional.of(something) : Optional.empty() should be optional.map(something_as_lambda)
  5. optional.isPresent() ? something : Optional.empty() should be optional.flatMap(something_as_lambda)

Activity

  1. added
    a:new-rule
    on Apr 10, 2018
  2. oowekyala commented on Apr 22, 2018

    @oowekyala
    Member

    Additionally, with java 9+,

    if (optional.isPresent()) {
      single_line1
    } else {
      single_line2
    }

    can be replaced by optional.ifPresentOrElse​(lambda1, lambda2)

  3. andreybleme commented on Oct 8, 2018

    @andreybleme

    @jsotuyod May I start to implement some of these rules?

  4. jsotuyod commented on Oct 8, 2018

    @jsotuyod
    MemberAuthor

    @andreybleme most definitively!

  5. andreybleme commented on Oct 8, 2018

    @andreybleme

    @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.

  6. jsotuyod commented on Oct 9, 2018

    @jsotuyod
    MemberAuthor

    @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.

  7. stefankatic commented on Jul 27, 2019

    @stefankatic

    Any progress on this?

  8. jsotuyod commented on Jul 30, 2019

    @jsotuyod
    MemberAuthor

    @stefankatic none, we haven't heard back from @andreybleme. Contributions are always welcomed.

  9. linusjf commented on Oct 1, 2019

    @linusjf

    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

  10. linusjf commented on Oct 1, 2019

    @linusjf

    It might be a good idea to revisit splitting the rule into multiple ones. That way, contributors can work independently on each piece.

  11. adangel commented on Oct 3, 2019

    @adangel
    Member

    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.

  12. linusjf commented on Oct 3, 2019

    @linusjf
  13. added this to the 7.0.0 milestone on Feb 11, 2022
  14. modified the milestones: 7.0.0, 7.x on Jan 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-rule

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions