-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Open
Code quality
Copy link
Description
Description
Copying and moving files are key functionality of any file manager, and detecting and correctly handling collisions is an important step in that process. There are several code quality issues with the collision processing in Files.
- The method
FilesystemHelpers.GetCollisionshould be calledFilesystemHelpers.GetCollisions, plural. - In that method, we should be building two lists:
nonConflictingItemsandconflictingItems. TheincomingItemslist is an unnecessary intermediary. - The custom
Zipmethod used to combinesourceanddestinationis inefficient: per item it performs an unnecessary delegate invocation and an unnecessary object allocation. We should just useEnumerable.Zip. Theindexis equally unnecessary. There really is no need to index into a list to retrieve the element that was just added to it. - In that first loop, the
collisionsdictionary is indexed into twice, where once is sufficient. It's standardTryAddmethod does the job just fine. - After showing the dialog, we're again building the
collisionsdictionary, now with the user's choices, and again the standardTryAddmethod is a better fit. - In the final loop of the
FilesystemHelpers.GetCollisionsmethod, we're scanning thecollisionsdictionary, instead of indexing into it. That makes the algorithm quadratic instead of linear.
The FilesystemHelpers.GetCollisions method is called in two places, CopyItemsAsync and MoveItemsAsync. Both have similar code to update the history with the result of the call.
- Again an inefficient custom
Zipmethod is used.Enumerable.Zipis a better fit. - The result of that
Zipmethod is then used to populate a dictionary, completely unnecessary. - For readability of the code, we should use meaningful variable names.
itemanditem2don't really help when trying to understand the code, nor doKeyandValue. - We should remove the unused
StopwatchinMoveItemsAsync.
Concerned code
- src\Files.App\Utils\Storage\Operations\FilesystemHelpers.cs
- src\Files.Shared\Extensions\EnumerableExtensions.cs
Gains
- Better readability of the code.
- Better performance, especially when copying or moving large numbers of items.
Requirements
- Fix the issues described above.
Comments
I have prepared a pull request.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
🆕 New