Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Optional::orElseThrow over Optional::get #2950

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

schlosna
Copy link
Contributor

Before this PR

#2946 (comment)

After this PR

==COMMIT_MSG==
Prefer Optional::orElseThrow over Optional::get
==COMMIT_MSG==

Possible downsides?

@changelog-app
Copy link

changelog-app bot commented Nov 12, 2024

Generate changelog in changelog/@unreleased

What do the change types mean?
  • feature: A new feature of the service.
  • improvement: An incremental improvement in the functionality or operation of the service.
  • fix: Remedies the incorrect behaviour of a component of the service in a backwards-compatible way.
  • break: Has the potential to break consumers of this service's API, inclusive of both Palantir services
    and external consumers of the service's API (e.g. customer-written software or integrations).
  • deprecation: Advertises the intention to remove service functionality without any change to the
    operation of the service itself.
  • manualTask: Requires the possibility of manual intervention (running a script, eyeballing configuration,
    performing database surgery, ...) at the time of upgrade for it to succeed.
  • migration: A fully automatic upgrade migration task with no engineer input required.

Note: only one type should be chosen.

How are new versions calculated?
  • ❗The break and manual task changelog types will result in a major release!
  • 🐛 The fix changelog type will result in a minor release in most cases, and a patch release version for patch branches. This behaviour is configurable in autorelease.
  • ✨ All others will result in a minor version release.

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Prefer Optional::orElseThrow over Optional::get

Check the box to generate changelog(s)

  • Generate changelog entry

@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Prefer Optional::orElseThrow over Optional::get
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Open question: Do we also want a more generic PreferOptionalOrElseThrow check to rewrite Optional::get & opt.get() to Optional::orElseThrow & opt.orElseThrow()

Copy link
Member

@pkoenig10 pkoenig10 Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be in favor of this. Especially to fix consumers that have already applied StreamFlatMapOptional.

@pkoenig10
Copy link
Member

I'm in favor of this, but would like @carterkozak to weigh in.

@carterkozak
Copy link
Contributor

I suppose I'd mentally modeled Optional#get as "I've verified that the optional is present, and would like to extract the value", and Optional#orElseThrow as "Extract the value, but throwing is reasonable if the optional is empty", and sort of discounted using orElseThrow without args in favor of providing a more specific SafeLoggable exception. I'd never put much thought into this distinction, and I'm not sure it makes sense to have two ways of doing basically the same thing. It's worth verifying if Optional support in NullAway guards get() and orElseThrow() in the same way, or only checks get() but considers orElseThrow() as a guard.

@pkoenig10
Copy link
Member

I've come to prefer orElseThrow() primarily for readability. With get(), it can be non-obvious that a particular piece of code can throw. This is especially valuable if:

  • The code that checks if the value is present is, for whatever reason, further away from the code that extracts the value.
  • The get() is called directly rather than with a method reference, so it's less obvious that the type of the value is an Optional. Particularly since we have a number of other types with get() methods, like Conjure aliases.

@pkoenig10
Copy link
Member

orElseThrow() was introduced in JDK-8140281 for exactly this reason. The author ultimately plans to deprecated get() in JDK-8160606 - although I suspect that will never happen.

@carterkozak
Copy link
Contributor

Given JDK-8160606, I agree. I suspect you're right that get may not actually be removed, but it seems reasonable to align with the JDKs design goals.

Unfortunately it seems that NullAway:CheckOptionalEmptiness only protects Optional.get(), not Optional.orElseThrow(). Only a few projects use this option, since we don't enable it by default anymore after some projects hit bugs: #2396
Probably worth looking into re-enabling, though. We've upgraded NullAway several times since that issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants