Skip to content

Conversation

@GurliGebis
Copy link
Contributor

If result is false, there is no reason to run the PathInternal.IsDirectorySeparator(...) function.
No matter what it returns, the result = result && ... will always yeild false, since result is false already.

So the entire if statement can be skipped, if result is false, since it cannot possible change result to a true.

If result is false, there is no reason to run the
PathInternal.IsDirectorySeparator function, since no matter what it
returns, the "result = result && ..." will always yeild false, since
result is false already.

So the entire if statement can be skipped, if result is false, since it
cannot possible change result to a true.
@ghost ghost added community-contribution Indicates that the PR has been added by a community member area-System.IO labels Jan 31, 2022
@ghost
Copy link

ghost commented Jan 31, 2022

Tagging subscribers to this area: @dotnet/area-system-io
See info in area-owners.md if you want to be subscribed.

Issue Details

If result is false, there is no reason to run the PathInternal.IsDirectorySeparator(...) function.
No matter what it returns, the result = result && ... will always yeild false, since result is false already.

So the entire if statement can be skipped, if result is false, since it cannot possible change result to a true.

Author: GurliGebis
Assignees: -
Labels:

area-System.IO, community-contribution

Milestone: -

bool result = Interop.Sys.LStat(fullPath, out Interop.Sys.FileStatus fileInfo) == Interop.Errors.ERROR_SUCCESS;
if (PathInternal.IsDirectorySeparator(fullPath[fullPath.Length - 1]))

if (result && PathInternal.IsDirectorySeparator(fullPath[fullPath.Length - 1]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you please also remove the code duplication and move this logic to Path.Exists?

Path.ExistsCore could return a value tuple (bool exists, bool isDirectory) or have an additional out bool isDirectory parameter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I understand what you mean?
What would the idea of returning if the path is a directory be, since that is not being used inside the Path.Exists function?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently the following logic is duplicated for both Windows and Unix:

if (result && PathInternal.IsDirectorySeparator(fullPath[fullPath.Length - 1]))
{
     result = isDirectory;
}

this check could be moved to File.Exists (the method that calls the methods that you have modified in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only code that is shared is the if statement, and since the part inside the if statement is dependent on stuff defined before the if statement, I don't think it is practical to try and move it into the normal Path.Exists.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not just about the if, it's also about duplicating the comments and keeping both versions in sync.

PTAL at the following commit: fd55a4d

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see - you are indeed correct.

I have integrated that into my branch and pushed it again.

Copy link
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you for your contribution @GurliGebis !

@GurliGebis
Copy link
Contributor Author

@adamsitnik you're very welcome.

Would it make sense to submit patches to cleanup coding style across the repo? (Things like if statements seems to be quiet inconsistent, with braces being used half of the times and stuff like that)

@adamsitnik
Copy link
Member

Would it make sense to submit patches to cleanup coding style across the repo? (Things like if statements seems to be quiet inconsistent, with braces being used half of the times and stuff like that)

To be honest with you I am not a big fan of refactoring PRs as it usually takes me more time as a reviewer to review them, rather than doing it myself (applying the changes can be automated, reviewing needs to examine every line).

Perhaps you would be interested in taking one of our up-for-grabs issues? You can find them here https://github.com/dotnet/runtime/issues?q=is%3Aissue+is%3Aopen+label%3Aup-for-grabs

These issues are something that we definitely want to fix and need help with doing that.

@GurliGebis
Copy link
Contributor Author

I'll take a look and see if there is anything I can help with on there 🙂

@adamsitnik adamsitnik merged commit 396886e into dotnet:main Jan 31, 2022
@adamsitnik adamsitnik added this to the 7.0.0 milestone Jan 31, 2022
@GurliGebis GurliGebis deleted the Optimize-ExistsCore branch January 31, 2022 19:01
@ghost ghost locked as resolved and limited conversation to collaborators Mar 3, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.IO community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants