-
-
Notifications
You must be signed in to change notification settings - Fork 351
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
refactor: fix code inspection manual array to collection copy #2224
Merged
pvojtechovsky
merged 10 commits into
INRIA:master
from
zielint0:refactor-fix-code-inspection-manual-array-to-collection-copy
Jul 18, 2018
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
47f60e7
refactor: move Main to testclasses - second try
zielint0 bd7ef7a
Revert "refactor: move Main to testclasses - second try"
zielint0 41fd248
Merge branch 'master' of https://github.com/INRIA/spoon
zielint0 d7220ba
Merge branch 'master' of https://github.com/INRIA/spoon
zielint0 136e66b
refactor: fix code inspection: performance: manual array to collectio…
zielint0 ce9d9dc
refactor addAll()
zielint0 0e7f84e
refactor addAll()
zielint0 68911e0
Fix import
zielint0 936981a
Restore Collections.addAll()
zielint0 d8b4ea8
Fix imports
zielint0 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why using the static call instead of using
this.inputs.addAll(input)
?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.
Motivation: use explicit and readable way to add all elements. The foreach is worse because developer must analyze it deeply and longer. And older loop (int i = 0; i < 10; i++) is the worst because it is slower to read and more error prone.
In other words: if it is possible to perform mass/one time action for all elements - do it. Avoid processing elements in loop one by one.
Hierarchy of processing elements (the lower, the more error prone and longer)
1 one call (addAll)
2 foreach (internal iteration)
3 for / while (external iteration)
4 manual processing
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.
https://m.youtube.com/watch?v=15X0qFtBqiQ
Use abstraction, high level mechanisms. Let the compiler do the dirty job. Exploit modern Java constructs.
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.
And I think that static call shows what is added to what in a clearer way.
But of course both styles work :)
The main point is to use addAll(), not looping.
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.
I agree with using
addAll
instead of the loop: my question was only about why not using the invocation from the instance directly.IMHO it's more readable than calling
Collections.addAll
with two arguments.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.
I tried to refactor from Collections.addAll() to this.input.addAll(input), but second variant cannot be compiled because of argument type which is: addInput(Object... input)
Collections.addAll() is:
addAll(Collection<? super T> c, T... elements)
this.input.addAll() is:
addAll(Collection<? extends E> c)
The exact mismatch in this.input.addAll(input) is:
In other words: Object... input is an array, so it can be used only in Collection<? super T> c, T... elements)
So the only way in this context is static method Collections.addAll().
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.
OK for this one then. But for the other ones you could have use the form instance.addAll() no?
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.
I used explicit and simple way in all cases: x.addAll(Arrays.asList(y)) :-)
No need for Collections.addAll()
I am ready to merge.