dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

Guard for IsNotEmpty(Guid)

Open aviita opened this issue 1 year ago • 2 comments

Overview

There should be Guard.IsNotEmpty(Guid) API to Guard against empty guids. With current API's the same guard can be achieved by Guard.IsTrue(guid != Guid.Empty);.

API breakdown

namespace CommunityToolkit.Diagnostics;

public static partial class Guard
{
    public static void IsNotEmpty([DoesNotReturnIf(Guid.Empty)] Guid value, [CallerArgumentExpression(nameof(memory))] string name = "", string message = "");
}

Note that I would also prefer to override the message in many cases, but without actually writing the argument name my self, so making also the message, with default value of empty string, but would only use it for the message if not empty (maybe).

Usage example

public class MyClass { private Guid _id;

public MyClass(Guid id)
{
    Guard.IsNotEmptyGuid(id);
    
    _id = id;
}

}

Breaking change?

No

Alternatives

Guard.IsTrue(guid == Guid.Empty);

Additional context

We are working with guids a lot in our project and would like to have cleaner guard for empty guids than Guard.IsTrue(guid == Guid.Empty);.

Verifying for empty Guid is pointless to have, so we only need guard for IsNotEmpty.

I am new to Guard API's, so I might be missing some best practices, but noticed this possible shortcoming in the package immediately when starting to use it in our project. Also searched for Stackoverflow and discussions, but there was no discussion to be found around Guid's and Guard.

Help us help you

Yes, I'd like to be assigned to work on this item

aviita avatar Dec 18 '23 11:12 aviita

@aviita Look at the Guard.IsNotDefault method, I think Guid.Empty == default(Guid) so this method will work as you want.

AlexRadch avatar Jan 16 '24 06:01 AlexRadch

@aviita Look at the Guard.IsNotDefault method, I think Guid.Empty == default(Guid) so this method will work as you want.

Thanks. Will do.

aviita avatar Jan 16 '24 10:01 aviita