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

Added nullable helper for IfNullOrWhiteSpace #12979

Merged
merged 2 commits into from
Sep 15, 2022

Conversation

patrickdemooij9
Copy link
Contributor

I noticed while working on a project that IsNullOrWhiteSpace always demanded that you null check the result even if you are 100% sure that result cannot be NULL. This is because there were no helper classes on it that could help with this. If defaultValue is not null, then we can be sure that the return value is not null. So I've added the helpers to the function.

Take the following code:

public void Test()
        {
            string? testNullString = null;
            string testString = "Hello";

            string test = testNullString.IfNullOrWhiteSpace("Hello")!;
            string? test2 = testNullString.IfNullOrWhiteSpace(null);
            string test3 = testString.IfNullOrWhiteSpace(null)!;
        }

This is how you would have to write it now. Even though we know test cannot be null, we still have to add the ! to the end of the statement to force it to accept a normal string.

However with the changes of the PR, you will be able to write it like this:

public void Test()
        {
            string? testNullString = null;
            string testString = "Hello";

            string test = testNullString.IfNullOrWhiteSpace("Hello");
            string? test2 = testNullString.IfNullOrWhiteSpace(null);
            string test3 = testString.IfNullOrWhiteSpace(null)!;
            string? test4 = testNullString.IfNullOrWhiteSpace(null);
        }

So now we don't have to add the ! after test as we already know that it will never return null.

@github-actions
Copy link

Hi there @patrickdemooij9, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@mikecp
Copy link
Contributor

mikecp commented Sep 15, 2022

Thanks @patrickdemooij9 for this nice little fix!
Cheers!

@mikecp mikecp merged commit 6d27454 into umbraco:v10/contrib Sep 15, 2022
@mikecp mikecp added community/pr release/no-notes This is too small to add to the release notes or fixed after a beta/RC category/dx Developer experience type/feature release/10.3.0 labels Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category/dx Developer experience community/pr release/no-notes This is too small to add to the release notes or fixed after a beta/RC release/10.3.0 type/feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants