-
Notifications
You must be signed in to change notification settings - Fork 39
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
StaticImportCheck: Add java.util.Collections
candidates
#23
StaticImportCheck: Add java.util.Collections
candidates
#23
Conversation
I think this makes sense in general. I do wonder though since I haven't used many of these often, it can be jarring to know where it's coming from. It does read nicer in most cases though (i.e. On another note: I liked that we had a vote for |
Yeah those are good examples of when it is not the best idea to statically import. An option could be to exclude the "generic" methods from statically importing. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should definitely NOT import methods like of, valueOf, create
, but here I think we are fine.
I've looked at the methods and usages of Collections.*
across the repositories and I must say no matter what the method is used, having Collections
in front of it does not add much context for readability. I do feel a bit less concerned about the implementation, but I still want to read the docs in order to understand better what it does.
Nice, you mention three methods that are listed as "bad import" in the |
java.util.Collections
as candidatejava.util.Collections
as candidate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed offline, I'm not 100% sure we should statically import all of these. Some of the method names are very short and generic, which could make a reader wonder whether (e.g.) a locally defined method is referenced. An example would be list
. I'm also not sure about min
and max
, as these could clash with static imports of Math.min
and Math.max
.
That said, all those checked*
, empty*
, singleton*
, synchronized*
and unmodifiable*
methods can indeed be statically imported without ambiguity.
(Added a small commit.)
@@ -109,6 +111,9 @@ void replacement() { | |||
" ImmutableSet.toImmutableSet();", | |||
" ImmutableSet.<String>toImmutableSet();", | |||
"", | |||
" boolean b = Collections.disjoint(ImmutableSet.of(), ImmutableSet.of());", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" boolean b = Collections.disjoint(ImmutableSet.of(), ImmutableSet.of());", | |
" Collections.disjoint(ImmutableSet.of(), ImmutableSet.of());", |
@@ -109,6 +111,9 @@ void replacement() { | |||
" ImmutableSet.toImmutableSet();", | |||
" ImmutableSet.<String>toImmutableSet();", | |||
"", | |||
" boolean b = Collections.disjoint(ImmutableSet.of(), ImmutableSet.of());", | |||
" Collections.reverse(new ArrayList<>(0));", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
" Collections.reverse(new ArrayList<>(0));", | |
" Collections.reverse(new ArrayList<>());", |
java.util.Collections
as candidatejava.util.Collections
as candidate
java.util.Collections
as candidatejava.util.Collections
as candidate
java.util.Collections
as candidatejava.util.Collections
candidates
Looked at these suggestions:
Based on these suggestions grep'ed in This selection of methods can be a good first list for |
3bdadf4
to
7748f2a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on these suggestions grep'ed in
picnic-java-all
to see whether they are used or not.
I don't think we should restrict ourselves to the set of methods currently used. Rather, let's define the full set that currently makes sense, so that we also provide a consistent experience in the future.
Rebased and added a counter-proposal; PTAL.
Suggested commit message:
Suggest static imports for most `java.util.Collections` methods (#23)
7748f2a
to
de984da
Compare
Rebased. The
Changes LGTM. |
Hmm, but eventually we should raise these (We should likely avoid |
Then let's import the methods statically. Will push it in a bit. W.r.t. the severity, it should indeed be raised to |
Yesterday we discussed this and decided to leave it as is, right? @Stephan202 Singleton will be rewritten to something else such that it doesn't occur in the code. |
@rickie yeah, let's just do it. Do you wanna resolve the conflicts? |
2877c6b
to
0f6549a
Compare
Rebased. |
Tnx! @EnricSala anything from your side? :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
0f6549a
to
898432a
Compare
Rebased and added a small commit. |
Updated suggested commit message:
|
Why would we now use a different message from previous commits? |
Updated. (Though given that suppression is possible, arguable "suggest" is better.) |
LGTM, let's merge 😄! |
Based on an offline discussion with @Stephan202 and a discussion in this PR, we decided to add
java.util.Collections
as static import candidate.Actually, we did specifically discuss
Collections.disjoint
and not the others of that class. However, there are many methods that look as good candidates for statically importing. Whether all these methods are good to import is still up for debate. So lets discuss what you think!Note: the support for exclusion of specific methods is not in this PR but in #22.