Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/main/java/dev/openfeature/sdk/HookSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,8 @@
var hook = hookContextPair.getKey();
var hookContext = hookContextPair.getValue();

Optional<EvaluationContext> returnedEvalContext = Optional.ofNullable(
hook.before(hookContext, data.getHints()))
.orElse(Optional.empty());
if (returnedEvalContext.isPresent()) {
Optional<EvaluationContext> returnedEvalContext = hook.before(hookContext, data.getHints());
if (returnedEvalContext != null && returnedEvalContext.isPresent()) {

Check warning on line 62 in src/main/java/dev/openfeature/sdk/HookSupport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Ensure this "Optional" could never be null and remove this null-check.

See more on https://sonarcloud.io/project/issues?id=open-feature_java-sdk&issues=AZ6W77MGgnWNSrzxjR7z&open=AZ6W77MGgnWNSrzxjR7z&pullRequest=1955
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

While this condition is correct, you could make the code more idiomatic and avoid the isPresent()/get() pattern by using ifPresent.

For example:

if (returnedEvalContext != null) {
    returnedEvalContext.ifPresent(returnedContext -> {
        // existing logic from inside the if block
    });
}

This separates the null-check (to handle non-standard hook implementations) from the optional-handling, which can improve readability.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ifPresent would use new Lambda or Method reference

var returnedContext = returnedEvalContext.get();
// yes, we want to check for reference equality here, this prevents recursive layered contexts
if (returnedContext != hookContext.getCtx() && !returnedContext.isEmpty()) {
Expand Down