Skip to content

Conversation

@CarloToso
Copy link
Contributor

@CarloToso CarloToso commented Dec 14, 2022

PR Summary

Replaces all instances of:

if (XXX == null)
{
    throw new ArgumentNullException(nameof(XXX));
}

with:

ArgumentNullException.ThrowIfNull(XXX)

PR Context

Proposed in #18596

The new API ArgumentNullException.ThrowIfNull allows you to omit the parameter name, in which case it will use the name of the argument passed in. The passed in argument usually is the parameter to be validated, so that's just the right name to use when calling ArgumentNullException.ThrowIfNull(argument).

Copy link
Contributor

@xtqqczze xtqqczze left a comment

Choose a reason for hiding this comment

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

Please address all feedback.

{
throw new ArgumentNullException(nameof(optionName));
}
ArgumentNullException.ThrowIfNull(optionName);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please revert this change in behaviour.

@xtqqczze
Copy link
Contributor

xtqqczze commented Dec 14, 2022

@CarloToso sorry missed this was WIP. Perhaps you could "convert to draft".

@CarloToso CarloToso marked this pull request as draft December 14, 2022 09:15
@CarloToso
Copy link
Contributor Author

I tried to replace the results of this search: throw new ArgumentNullException(, tell me if I forgot something.

@pull-request-quantifier-deprecated

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


Quantification details

Label      : Extra Large
Size       : +257 -492
Percentile : 91.63%

Total files changed: 122

Change summary by file extension:
.cs : +257 -492

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.

@CarloToso CarloToso marked this pull request as ready for review December 14, 2022 12:28
@CarloToso CarloToso requested a review from anmenaga as a code owner December 14, 2022 12:28
@CarloToso CarloToso changed the title [WIP]throw new argument null exception(nameof()) -> ArgumentNullException.ThrowIfNull() throw new ArgumentNullException(nameof()) -> ArgumentNullException.ThrowIfNull() Dec 14, 2022
@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Dec 14, 2022
@iSazonov iSazonov assigned iSazonov and unassigned PaulHigin Dec 14, 2022
@iSazonov iSazonov changed the title throw new ArgumentNullException(nameof()) -> ArgumentNullException.ThrowIfNull() Replace ArgumentNullException(nameof()) -> ArgumentNullException.ThrowIfNull() Dec 14, 2022
@iSazonov iSazonov merged commit 2ddc63f into PowerShell:master Dec 14, 2022
@iSazonov
Copy link
Collaborator

iSazonov commented Dec 14, 2022

@CarloToso Thanks!

I found ~71 lines with " ArgumentNullException(" patterns still not fixed. Some ones more complex. We could think about split some ones on two - for null, NullOrEmpty and for range (ArgumentOutOfRangeException).

See also dotnet/roslyn-analyzers#6293 that we could fix too.

Also we could update/replace internal static class Requires with new methods.

@CarloToso
Copy link
Contributor Author

Should we do something for PSArgumentNullException( and PSTraceSource.NewArgumentNullException( ?

@CarloToso CarloToso deleted the throw-new-ArgumentNullException(nameof()) branch December 14, 2022 14:38
@iSazonov
Copy link
Collaborator

Should we do something for PSArgumentNullException( and PSTraceSource.NewArgumentNullException( ?

Yes, it's worth it.

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 Large

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants