Skip to content

Add Ruby on Rails-like string members #26677

@MisinformedDNA

Description

@MisinformedDNA

When I want to know if a value exists in a string, I find myself wanting a couple Ruby on Rails-like methods to work with. I find them clearer and more obvious for typical tasks like input validations.

public class String
{
    public bool IsEmpty() => Length == 0;
}

public static class StringExtensions
{
    // need to be extension methods to avoid `NullReferenceExceptions`
    public static bool IsNullOrEmpty(this string value) => string.IsNullOrWhiteSpace(value);
    public static bool IsNullOrWhiteSpace(this string value) => string.IsNullOrWhiteSpace(value);

    // avoids logical negation
    public static bool IsPresent(this string value) => !string.IsNullOrWhiteSpace(value);
}

IsEmpty()

The most obvious way of determining if a string is empty is

value == ""

But this violates CA1820. Developers are instead instructed to use Length (for .NET 1.X) or IsNullOrEmpty (.NET 2.0+).

Length is non-obvious and IsNullOrEmpty is a static that does not flow well. Additionally, they aren't functionally equivalent since

((string)null).Length == 0 throws an exception while IsNullOrEmpty(null) returns true

Suggestion: Add instance method IsEmpty()

IsNullOrEmpty()/IsNullOrWhiteSpace()

It's difficult to read this left to right !IsNullOrEmpty(value). It can be even more difficult to determine what the developer actually intended. I can never write this left to right either. It usually comes out in this order:

value, move cursor to left, string.IsNullOrWhiteSpace(, move cursor to right ), (optional) move cursor to left, !

Yuck!

Suggestion: Add equivalent extension methods instead

Microsoft.Azure.Devices makes use of a similar extension method

IsPresent()

Logical negations present additional steps in brain power, especially when null is involved. Compare "Is a value present?" to "Is this value not null and also not whitespace?"

Suggestion: Add extension method IsPresent (or, alternatively, IncludesNonWhiteSpaceChars or HasValue or HasVisibleChars)

Use Cases

Replace

public string Original(string id)
{
    if (id == null)
        return "NULL";
    if (string.IsNullOrEmpty(id))
        return "EMPTY";
    if (string.IsNullOrWhiteSpace(id))
        return "WHITE_SPACE";
    if (!string.IsNullOrWhiteSpace(id))
        return "VISIBLE_STRING";
    ...
}

with

public string Suggested(string id)
{
    if (id == null)
        return "NULL";
    if (id.IsEmpty())
        return "EMPTY";
    if (id.IsNullOrWhiteSpace())
        return "WHITE_SPACE";
    if (!id.IsNullOrWhiteSpace() && id.IsPresent())    // both expressions are equivalent
        return VISIBLE_STRING;
    ...
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions