Enum.Source.Generator icon indicating copy to clipboard operation
Enum.Source.Generator copied to clipboard

Should returning false rather than throwing an exception be better behaviour?

Open Ajay100000 opened this issue 2 years ago • 0 comments

The code looks very good, but I think it is more useful if 'IsDefinedFast(string states)' returns false rather than throwing an exception. This means that if the string is not defined in the enum collection and we get back 'false' and can act on that in the if statement.

Here is my example:

// currently throws an exception (would be better if it returned false)
if (UserTypeTestEnumExtensions.IsDefinedFast("Gien"))
{
    System.Console.WriteLine("'Gien' is defined");
}
else
{
    System.Console.WriteLine("Not defined is 'Gien'"); 
}


Here is the pseudo code for what I am suggesting:

public static bool IsDefinedFast(string states)
{
    return states switch
    {
        nameof(UnitTests.UserTypeTest.Men) => true,
        nameof(UnitTests.UserTypeTest.Women) => true,
        nameof(UnitTests.UserTypeTest.None) => true,
        _ => false
    };
}

This is only for this specific scenario that I suggest returning false.

What do you think?

Ajay100000 avatar Sep 09 '22 14:09 Ajay100000