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

Add methods to KiwiResources to return non-null objects for method chaining #1166

Closed
sleberknight opened this issue Jul 22, 2024 · 2 comments · Fixed by #1183
Closed

Add methods to KiwiResources to return non-null objects for method chaining #1166

sleberknight opened this issue Jul 22, 2024 · 2 comments · Fixed by #1183
Assignees
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Milestone

Comments

@sleberknight
Copy link
Member

sleberknight commented Jul 22, 2024

KiwiResources has a collection of overloaded methods named verifyExistence which have different signatures. Half of them accept T resourceEntity and the other half accept Optional<T> resourceEntity.

All of them throw a JaxrsNotFoundException when the object does not exist, either because resourceEntity is null or is an empty Optional.

The methods that accept Optional<T> return a (non-null) T, which allows code like:

Optional<HealthCheckExecution> latest = healthCheckDao.findLatestExecution();

var executionId = KiwiResources.verifyExistence(latest).getId();  // safe to call getId

But, if you have a possibly nullable object, you must write this code:

HealthCheckExecution latest = healthCheckDao.findLatestExecution();

KiwiResources.verifyExistence(latest);  // can't call getId here because return type is void
var executionId = latest.getId();  // this is safe

The above code is safe, but code analysis tools may still report a possible NPE from latest.getId(), because they don't understand that KiwiResources.verifyExistence will either return a (non-null) object or throw an exception. So then you have to either suppress (e.g., using an annotation, or in Sonar's web UI, etc.) the warning, or wrap latest with Objects.requireNonNull even though it can't ever be null if verifyExistence returns without throwing (or just ignore it).

This issue addresses the method-chaining by introducing new methods similar to the void-returning verifyExistence methods, or possibly just modifying the existing methods to return the same object when it is not null. I think this should be binary compatible since no existing code can possibly have a return type and compile.

A second issue will be created to annotate all the verifyExistence methods with an annotation to indicate that they should not ever return null. This will make static analyzers happy, or should anyway.

@sleberknight sleberknight added the new feature A new feature such as a new class, method, package, group of classes, etc. label Jul 22, 2024
@sleberknight sleberknight changed the title Add methods to KiwiResources to return non-null objects for method chaining Add/update methods to KiwiResources to return non-null objects for method chaining Jul 22, 2024
@sleberknight sleberknight added this to the 4.3.0 milestone Jul 22, 2024
@sleberknight
Copy link
Member Author

Never mind, changing the return type is not binary compatible regardless.

Change result type (including void) --> Breaks compatibility

source: https://github.com/eclipse-platform/eclipse.platform/blob/master/docs/Evolving-Java-based-APIs-2.md#evolving-api-interfaces---api-methods

Also:

Changing the result type of a method, or replacing a result type with void, or replacing void with a result type, has the combined effect of deleting the old method and adding a new method with the new result type or newly void result

source: https://docs.oracle.com/javase/specs/jls/se17/html/jls-13.html#jls-13.4.15

This means we must add new methods if we don't want to go to a new major version. And since we have many Dropwizard services that definitely use the void KiwiResources.verifyExistence(T obj) methods, we don't want any of these to break.

So, the question becomes: what should the new methods be named? I asked "a friend" (ChatGPT 4o) what "it" thought and got the following:

  1. verifyAndReturn
  2. checkAndReturn
  3. validateAndReturn
  4. verifyPresence
  5. ensureExistence

I said the names must contain "verify" and "existence" and got:

  1. verifyExistenceAndReturn
  2. verifyAndReturnExistence
  3. returnVerifiedExistence
  4. ensureAndReturnExistence
  5. validateExistenceAndReturn

I then told it that 3, 4, and 5 did not contain both "verify" existence" and got an "apology" and these suggestions:

  1. verifyExistenceAndReturn
  2. verifyAndReturnExistence
  3. verifyExistenceWithReturn
  4. verifyExistenceAndProvide
  5. verifyExistenceReturning

The only thing I came up with before asking ChatGPT was verifyExistenceAndReturn, but I also think verifyExistenceWithReturn and verifyExistenceReturning would be somewhat acceptable.

Overall I think verifyExistenceAndReturn is probably the simplest and most descriptive name for the new methods.

@sleberknight sleberknight changed the title Add/update methods to KiwiResources to return non-null objects for method chaining Addmethods to KiwiResources to return non-null objects for method chaining Jul 22, 2024
@sleberknight sleberknight changed the title Addmethods to KiwiResources to return non-null objects for method chaining Add methods to KiwiResources to return non-null objects for method chaining Jul 22, 2024
@sleberknight sleberknight self-assigned this Aug 9, 2024
@dsingley
Copy link

dsingley commented Aug 9, 2024

+1: verifyExistenceAndReturn

sleberknight added a commit that referenced this issue Aug 9, 2024
* Add new verifyExistenceAndReturn methods to KiwiResources to
  return non-null objects for method chaining
* Rearrange method order so they are grouped by error message
  (no message, static message, message template and args).

Clsoes  #1166
dsingley pushed a commit that referenced this issue Aug 10, 2024
* Add new verifyExistenceAndReturn methods to KiwiResources to return
non-null objects for method chaining
* Rearrange method order so they are grouped by error message (no
message, static message, message template and args).

Clsoes  #1166
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature A new feature such as a new class, method, package, group of classes, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants