Skip to content

Conversation

@daxian-dbw
Copy link
Member

PR Summary

Revert a few changes to not use 'ArgumentNullException.ThrowIfNull'.
They are all combined checks on the arguments, which should be kept as it was.

PR Checklist

@daxian-dbw daxian-dbw requested a review from anmenaga as a code owner February 14, 2023 19:24
@ghost ghost assigned PaulHigin Feb 14, 2023
@daxian-dbw daxian-dbw requested a review from iSazonov February 14, 2023 19:25
@daxian-dbw daxian-dbw assigned daxian-dbw and unassigned PaulHigin Feb 14, 2023
@daxian-dbw daxian-dbw added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Feb 14, 2023
@pull-request-quantifier-deprecated

This PR has 19 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +7 -12
Percentile : 7.6%

Total files changed: 6

Change summary by file extension:
.cs : +7 -12

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

ArgumentNullException.ThrowIfNull(value, paramName);

if (value.Count == 0)
if (value is null || value.Count == 0)
Copy link
Collaborator

Choose a reason for hiding this comment

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

As I pointed already in another PR, this generates worst code.
Also in .Net 8 Preview 1 we will get ArgumentOutOfRangeException.ThrowIfZero (and more) so that the code will be:

ArgumentNullException.ThrowIfNull(value, paramName);
ArgumentNullException.ThrowIfZero(value.Count);

Copy link
Member Author

@daxian-dbw daxian-dbw Feb 15, 2023

Choose a reason for hiding this comment

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

The difference in generated code doesn't always matter, and you cannot use it as the single criteria. In this case, the original code is more readable.

As far as I can found in the dotnet/runtime repo, it's ArgumentOutOfRangeException.ThrowIfZero, not ArgumentNullException.ThrowIfZero. It throws a different exception.
Also, even in case of ArgumentOutOfRangeException.ThrowIfZero(value.count), I don't know what will be used as the parameter name, "value.count" I guess?

Copy link
Contributor

@CarloToso CarloToso Feb 16, 2023

Choose a reason for hiding this comment

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

You are right it will throw a different exception, I don't know if it will be a problem (maybe we just have to change the tests).

From dotnet/runtime#69590
public static void ThrowIfZero<T>(T value, [CallerArgumentExpression("value")] string? paramName = null) it seems can set any string as parameter name

Copy link
Collaborator

Choose a reason for hiding this comment

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

The original code is not correct. There are not too few places in our code where an exception is called that is not what it should be. This is not even a breaking change, as it is not a functional exception, and it is worth correcting it for the correct one. If someone even uses this behavior in their code, it is catastrophically bad code.

I don't know what will be used as the parameter name, "value.count" I guess?

Yes, as the attribute name (CallerArgumentExpression) says expression is used as "name". It is more informative and more correct. (Otherwise this API would never have been approved.)

So I think we should fix such protective (non-functional) exceptions to the correct ones in whole code base.

Copy link
Member Author

@daxian-dbw daxian-dbw Feb 17, 2023

Choose a reason for hiding this comment

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

So I think we should fix such protective (non-functional) exceptions to the correct ones in whole code base.

That will need to be discussed and approved by all maintainers. If you insist on fixing those exceptions, then please open an issue on it and mark it with Review - Maintainer.

ArgumentOutOfRangeException won't be the right exception either. If you insist to be accurate about the exception, then an ArgumentException with a customized error message should be the way to go.

In my personal view, changing those exceptions is not worth the time (discussion, making the change, and code review). But if all other maintainers agree to make the change, then I'm fine to replace the current code with a customized ArgumentException.

Copy link
Member Author

Choose a reason for hiding this comment

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

I already explained in #19151 (comment) why it's arguably incorrect. I'm not trying to persuade you, just to clarify my opinion.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm trying to figure out what to demand from other contributors so we don't waste time later. So you'd better convince me. 😃
Right now I don't see that a ArgumentException message would be better than an ArgumentOutOfRangeException. (If I understand correctly you have no objection to the exception class type used but only to content of message.)
While I'm on vacation and have no opportunity to experiment. I'll try it a week later.

Copy link
Member Author

Choose a reason for hiding this comment

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

A custom message will be absolutely better than 'paramName' must be a non-zero value. (Parameter 'paramName'), which is what ArgumentOutOfRangeException.ThrowIfZero gives you.

In this particular case, the message should be something like 'paramName' must be a non-empty collection. (Parameter 'paramName')

you have no objection to the exception class type

I'm fine changing the type of exception to be thrown. That's a bucket 3 breaking change.

Copy link
Contributor

Choose a reason for hiding this comment

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

I took a look at the history of this API. It was created by Stephan Toub and updated by him. It is hard to suspect him of negligence. I think this is intentional since the exact API was rejected as I mentioned above. So after a while this pattern may become a frequently used one.

To be clear, ClientWebSocketOptions.SetBuffer(Int32, Int32, ArraySegment<Byte>) is a public API introduced in .NET Framework 4.5. In any case, please could we should start discussion in a new issue.

Copy link
Contributor

@xtqqczze xtqqczze Feb 24, 2023

Choose a reason for hiding this comment

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

I took a look at the history of this API. It was created by Stephan Toub and updated by him. It is hard to suspect him of negligence. I think this is intentional since the exact API was rejected as I mentioned above. So after a while this pattern may become a frequently used one.

To be clear, ClientWebSocketOptions.SetBuffer(Int32, Int32, ArraySegment<Byte>) is a public API introduced in .NET Framework 4.5. In any case, please could we continue discussion in a new issue.

Copy link
Contributor

@PaulHigin PaulHigin left a comment

Choose a reason for hiding this comment

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

LGTM

@daxian-dbw daxian-dbw merged commit 77fcd47 into PowerShell:master Feb 22, 2023
@daxian-dbw daxian-dbw deleted the updates branch February 22, 2023 21:31
@ghost
Copy link

ghost commented Mar 14, 2023

🎉v7.4.0-preview.2 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log Extra Small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants