Skip to content

Replaces the single-iteration foreach loops#48

Merged
AArnott merged 4 commits into
AArnott:masterfrom
scottdorman:master
Sep 28, 2020
Merged

Replaces the single-iteration foreach loops#48
AArnott merged 4 commits into
AArnott:masterfrom
scottdorman:master

Conversation

@scottdorman

@scottdorman scottdorman commented Sep 27, 2020

Copy link
Copy Markdown
Contributor

This will close #47.

@codecov-commenter

codecov-commenter commented Sep 27, 2020

Copy link
Copy Markdown

Codecov Report

Merging #48 into master will increase coverage by 2.02%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #48      +/-   ##
==========================================
+ Coverage   88.88%   90.90%   +2.02%     
==========================================
  Files           9        9              
  Lines         279      275       -4     
==========================================
+ Hits          248      250       +2     
+ Misses         31       25       -6     
Impacted Files Coverage Δ
src/Validation/Requires.cs 96.29% <100.00%> (+5.22%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a6a5350...aa3d15a. Read the comment docs.

@scottdorman

Copy link
Copy Markdown
Contributor Author

Not sure how you want to handle the codecov issue. As far as I can tell, it's saying that the coverage went down because there were lines of code removed. When I look at the diff, it looks like the new lines are being fully covered by tests.

@AArnott AArnott left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks. Just one change and we can merge.

Comment thread src/Validation/Requires.cs Outdated
}

if (!hasElements)
if (!values.GetEnumerator().MoveNext())

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

The old code would try casting the IEnumerator to IDisposable and dispose it if it did. It would do this in a try/finally to ensure it happened. We lost that in this method. Can you add it back?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I can make this change, but I double-checked the code and didn't see anywhere that had it wrapped.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm not sure what you mean. The try finally behavior I'm referring to comes automatically from the compiler by virtue of the foreach loop.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LOL. Oh! I thought you mean that it was explicitly wrapped in a try..finally. 🤦

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't think to wrap it initially because IEnumerator doesn't implement IDisposable, although IEnumerator<T> does.

@scottdorman

Copy link
Copy Markdown
Contributor Author

Ok. Definitely not sure what you want to do about the codecov/patch failure here. It looks like it's complaining because the new call to dispose of the enumerator isn't being covered by a unit test. I can try and fabricate a unit test that will hit if you want me to though.

@scottdorman

Copy link
Copy Markdown
Contributor Author

I didn't mean to push that last commit (87e25c5) into this PR. :( Should I revert it?

@AArnott

AArnott commented Sep 28, 2020

Copy link
Copy Markdown
Owner

I didn't mean to push that last commit (87e25c5) into this PR. :( Should I revert it?

I wouldn't push a revert commit, but rather force-push the rollback so it's as if it never happened.

Comment thread src/Validation/Requires.cs Outdated
Comment on lines 201 to 207
finally
{
throw new ArgumentException(Format(Strings.Argument_EmptyArray, parameterName), parameterName);
if (enumerator is IDisposable disposable)
{
disposable.Dispose();
}
}

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

tip: a more succinct syntax for try/finally based disposal in this case can be achieved like this:

IEnumerator enumerator = values.GetEnumerator();
using (enumerator as IDisposable)
{
    if (!values.GetEnumerator().MoveNext())
    {
        throw new ArgumentException(Format(Strings.Argument_EmptyArray, parameterName), parameterName);
    }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That gets the enumerator twice though. Wouldn't we want to avoid that?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Whoops. Well, that bug was already in your code. :) I just copied it in. Yes, let's avoid calling GetEnumerator() twice by actually using your enumerator local.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Shoot! You're right. It was. ☹️ I missed that when I changed it to the try...finally.

@AArnott AArnott left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks

@AArnott AArnott merged commit bb6a8e9 into AArnott:master Sep 28, 2020
AArnott added a commit that referenced this pull request May 2, 2023
Add -y switch to choco install
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

Successfully merging this pull request may close these issues.

Replace the single-iteration foreach loops

3 participants