Runtime: Add `Ruby on Rails`-like string members

Created on 2 Jul 2018  路  5Comments  路  Source: dotnet/runtime

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;
    ...
}
api-suggestion area-System.Runtime

Most helpful comment

I don't think we should add such members. From my perspective, it is very non-.NET-like to make a call like id.IsNullOrWhiteSpace()... it's very unintuitive to me to make a method call on something to check it for null.

All 5 comments

I don't think we should add such members. From my perspective, it is very non-.NET-like to make a call like id.IsNullOrWhiteSpace()... it's very unintuitive to me to make a method call on something to check it for null.

But it's not entirely without precedent, right?

((int?)value).HasValue

is functionaly equivalent to just comparing against null

value != null

Nullable<T> is not a reference type. HasValue is actually an instance member.

In the meantime, I created a library that does some of this but focuses on adding some Ruby and Rails methods as-is and staying true to their implementation.

Since the main focus of the proposal is to add extension methods which operate on null, and that's generally against our guidance, this doesn't seem like anything that will be pursued.

Was this page helpful?
0 / 5 - 0 ratings