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

[API Proposal]: ArgumentException.ThrowIfNullOrWhitespace #76529

Closed
linkdotnet opened this issue Oct 3, 2022 · 4 comments
Closed

[API Proposal]: ArgumentException.ThrowIfNullOrWhitespace #76529

linkdotnet opened this issue Oct 3, 2022 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime

Comments

@linkdotnet
Copy link

Background and motivation

.NET 7 introduced a new helper method: ArgumentException.ThrowIfNullOrEmpty(), which throws an ArgumentNullException when the string is null and ArgumentException when the string is empty.

In lots of use-case scenarios an empty strings is equal to a string which purely consist out of whitespaces. Let's say a GitHub username is invalid either way: Null, empty or full of blanks or tabs. There are lots of scenarios where user input is only valid if there are printable characters.

To remove the boiler plate code and align how user would write code a new ThrowIfNullOrWhitespace could be introduced.

API Proposal

public class ArgumentException
{
    public static void ThrowIfNullOrWhitespace(string argument, [CallerArgumentExpression("argument")] string? paramName = null)
    {
        if (string.IsNullOrWhitespace(argument))
        {
            ArgumentNullException.ThrowIfNull(argument, paramName);
            throw new ArgumentException("String should not be empty or have only whitespaces", paramName);
        }
    }
}

API Usage

public void AddUser(string? username)
{
    ArgumentException.ThrowIfNullOrWhitespace(username);
}

Alternative Designs

No response

Risks

No response

@linkdotnet linkdotnet added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Oct 3, 2022
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 3, 2022
@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@ghost
Copy link

ghost commented Oct 3, 2022

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Background and motivation

.NET 7 introduced a new helper method: ArgumentException.ThrowIfNullOrEmpty(), which throws an ArgumentNullException when the string is null and ArgumentException when the string is empty.

In lots of use-case scenarios an empty strings is equal to a string which purely consist out of whitespaces. Let's say a GitHub username is invalid either way: Null, empty or full of blanks or tabs. There are lots of scenarios where user input is only valid if there are printable characters.

To remove the boiler plate code and align how user would write code a new ThrowIfNullOrWhitespace could be introduced.

API Proposal

public class ArgumentException
{
    public static void ThrowIfNullOrWhitespace(string argument, [CallerArgumentExpression("argument")] string? paramName = null)
    {
        if (string.IsNullOrWhitespace(argument))
        {
            ArgumentNullException.ThrowIfNull(argument, paramName);
            throw new ArgumentException("String should not be empty or have only whitespaces", paramName);
        }
    }
}

API Usage

public void AddUser(string? username)
{
    ArgumentException.ThrowIfNullOrWhitespace(username);
}

Alternative Designs

No response

Risks

No response

Author: linkdotnet
Assignees: -
Labels:

api-suggestion, area-System.Runtime, untriaged

Milestone: -

@GSPP
Copy link

GSPP commented Oct 12, 2022

This is certainly a reasonable piece of logic to have and I understand the desire to have a helper method for this. That being said, this is a very specific use case that isn't common enough across the .NET ecosystem to warrant a built-in helper. All kinds of variations of this kind of logic come to mind.

I solve the "whitespace problem" in user input like this:

var input = ...;

var trimmed = input.Trim();
if (trimmed.Length == 0) ... //Error case

UseUserInput(trimmed); //Reuse the trimmed string for further processing

This is the form of logic that my projects have needed many times. My logic is slightly different from yours, making my point.

I have a helper method just for this, and it would not have any business being in the BCL because it is overly specific.

@linkdotnet
Copy link
Author

Fair point and I do understand your reasoning. That said, we can close the proposal.

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Oct 13, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Nov 12, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Runtime
Projects
None yet
Development

No branches or pull requests

3 participants