Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable more nullable annotations in WebCmdlets #19359

Merged
merged 42 commits into from Apr 24, 2023

Conversation

CarloToso
Copy link
Contributor

@CarloToso CarloToso commented Mar 17, 2023

PR Summary

Enable more nullable comments in WebCmdlets, please review carefully.

PR Context

Follow up to #19291

PR Checklist

…Cmdlet/Common/ContentHelper.Common.cs

Co-authored-by: Ilya <darpa@yandex.ru>
{
bool result = false;
try
{
ArgumentException.ThrowIfNullOrEmpty(characterSet);
Copy link
Collaborator

@iSazonov iSazonov Mar 30, 2023

Choose a reason for hiding this comment

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

Hmm, I think it is wrong. Encoding.GetEncoding(null) returns OS default but Encoding.GetEncoding(string.Empty) throw. We should investigate the last case and if it is a bug we should fix in another 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.

I think the desired behaviour is that both cases (characterSet == null; characterSet == string.Empty) should should return result = false and encoding = UTF8 (the default). Do you agree?

Copy link
Collaborator

Choose a reason for hiding this comment

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

The code is in try-catch. So it makes no sense. Please remove.

Copy link
Contributor Author

@CarloToso CarloToso Mar 30, 2023

Choose a reason for hiding this comment

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

I know it doesn't make sense, I added it to avoid a possible null reference error for characterSet, could you point me to a better solution?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Null reference error? In TryGetEncoding? It is impossible since Encoding.GetEncoding(null) returns OS default, for string.Empty try-catch will catch and set default too. Also it will set default if .Net doesn't know a value in characterSet.

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 warning before the last 2 commits was Possible null reference argument for parameter 'name' in 'Encoding Encoding.GetEncoding(string name)'., and the build fails on warnings

Copy link
Collaborator

Choose a reason for hiding this comment

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

Since [System.Text.Encoding]::GetEncoding($null) return OS default without exception I suggest to open new issue in .Net.
Today we could use a workaround with ! operator and comment with reference to the new .Net issue.

Copy link
Contributor Author

@CarloToso CarloToso Apr 1, 2023

Choose a reason for hiding this comment

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

I looked a bit more into it
Test:

$source = @"
using System;
using System.Text;

    public class EncodingTest
    {
        public static Encoding GetEncoding1(string name)
        {
            return Encoding.GetEncoding(name);
        }

        public static Encoding GetEncoding2(int name)
        {
            return Encoding.GetEncoding(name);
        }
    }
"@

Add-Type -TypeDefinition $source -Language CSharp -ReferencedAssemblies netstandard, System.Text.Encoding

[EncodingTest]::GetEncoding1($null)
#--> ERROR
[EncodingTest]::GetEncoding2($null)
#--> windows-1252

[EncodingTest]::GetEncoding1("")
#--> ERROR
[EncodingTest]::GetEncoding2("")
#--> windows-1252
[EncodingTest]::GetEncoding2(0)
#--> windows-1252

In our case we are using Encoding.GetEncoding(string name) so it throws an error with null or empty that gets caught, we don't have a bug everything works as planned. Unfortunalely Encoding.GetEncoding(string name) with null gives us a warning, as you said we could use ! operator.

If Encoding.GetEncoding(string name) with name == null returned windows-1252 that wouldn't be a desired behaviour for us in this case, because we want to default to UTF-8

@CarloToso
Copy link
Contributor Author

Unrelated error

@iSazonov iSazonov added the CL-CodeCleanup Indicates that a PR should be marked as a Code Cleanup change in the Change Log label Apr 1, 2023
@iSazonov iSazonov changed the title WebCmdlets more nullable comments Enable more nullable comments in WebCmdlets Apr 1, 2023
@iSazonov iSazonov changed the title Enable more nullable comments in WebCmdlets Enable more nullable annotations in WebCmdlets Apr 1, 2023
@pull-request-quantifier-deprecated

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


Quantification details

Label      : Medium
Size       : +86 -69
Percentile : 51%

Total files changed: 14

Change summary by file extension:
.cs : +86 -69

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.

@ghost ghost added the Review - Needed The PR is being reviewed label Apr 11, 2023
@ghost
Copy link

ghost commented Apr 11, 2023

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@CarloToso
Copy link
Contributor Author

@iSazonov can you merge this PR?

@iSazonov iSazonov enabled auto-merge (squash) April 22, 2023 18:15
@iSazonov iSazonov disabled auto-merge April 22, 2023 19:06
@iSazonov
Copy link
Collaborator

@CarloToso Please look test fails.

@ghost ghost removed the Review - Needed The PR is being reviewed label Apr 22, 2023
@CarloToso
Copy link
Contributor Author

@iSazonov all tests pass

@iSazonov iSazonov merged commit 543b20b into PowerShell:master Apr 24, 2023
40 checks passed
@CarloToso CarloToso deleted the nullable-webcmdlets branch May 22, 2023 17:53
@ghost
Copy link

ghost commented Jun 29, 2023

🎉v7.4.0-preview.4 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 Medium
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants