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

modernizer is at odds with guava recommendations #40

Open
hgschmie opened this issue Nov 16, 2015 · 5 comments
Open

modernizer is at odds with guava recommendations #40

hgschmie opened this issue Nov 16, 2015 · 5 comments

Comments

@hgschmie
Copy link
Contributor

According to https://code.google.com/p/guava-libraries/wiki/PreconditionsExplained, the guideline if using Guava in JDK7+ is

--- snip ---
We preferred rolling our own preconditions checks over e.g. the comparable utilities from Apache Commons for a few reasons. Piotr Jagielski discusses why he prefers our utilities, but briefly:

After static imports, the Guava methods are clear and unambiguous. checkNotNull makes it clear what is being done, and what exception will be thrown.
checkNotNull returns its argument after validation, allowing simple one-liners in constructors: this.field = checkNotNull(field).
Simple, varargs "printf-style" exception messages. (This advantage is also why we recommend continuing to use checkNotNull over Objects.requireNonNull introduced in JDK 7.)
--- snip ---

which clashes with https://github.com/andrewgaul/modernizer-maven-plugin/blob/master/src/main/resources/modernizer.xml#L597-L607

@electrum
Copy link
Contributor

electrum commented Nov 6, 2016

As a bit of data, I checked the Presto code base, where we previously converted from checkNotNull to requireNonNull. We have over 4600+ instances of requireNonNull and less than 25 which have a non-constant message, of which most should actually be checkArgument, since they're doing something like this:

Type type = requireNonNull(types.get(symbol), "No type for symbol " + symbol);

Also note that you can pass a Supplier for the message:

Type type = requireNonNull(types.get(symbol), () -> "No type for symbol " + symbol);

Thus, my opinion is that the consistency of using the JDK method everywhere outweighs the advantages of the Guava format string (which does have the nice feature of being safe even if you mess up the format args).

@gaul
Copy link
Owner

gaul commented Mar 5, 2022

@kevinb9n any thoughts on this? I would like to close this issue if not.

@kevinb9n
Copy link

Sorry I missed that question.

So yeah, I think checkNotNull has some advantages over requireNonNull, especially if the codebase also uses checkArgument/checkState. For that matter, I also believe in the value of Guava's Preconditions.checkNotNull / Verify.verifyNotNull distinction, so that a stack trace always portrays very clearly whether it is the caller's fault or not. But I doubt many projects care that much about exception hygiene, so <shrug>.

I know there are a lot of projects that were only using the parts of Guava that now have JDK analogues, and it's easy to understand why they would want to shed all their remaining Guava usages and jettison the dependency.

@hgschmie
Copy link
Contributor Author

I wonder if adding Supplier variants to the various Verify/Precondition methods would be possible. E.g.

Preconditions.checkNotNull(T obj, Supplier<String> messageSupplier), similar to what exists for requireNonNull. Having those for checkNotNull/checkArgument/checkState and Verify.verify/verifyNotNull might be valuable.

@kevinb9n
Copy link

@hgschmie see google/guava#5927

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

No branches or pull requests

4 participants