-3

I have an enum that looks like this:

public enum MO
{
    Learn = 0,
    Practice = 1,
    Quiz = 2,
    SM2 = 3
}

public static partial class Extensions
{

    public static bool IsLearn(this MO mode)
    {
        return mode switch
        {
            MO.Learn => true,
            MO.Practice => false,
            MO.Quiz => false,
            MO.SM2 => false,
            _ => throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
        };
    }

    public static bool IsPractice(this MO mode)
    {
        return mode switch
        {
            MO.Learn => false,
            MO.Practice => true,
            MO.Quiz => false,
            MO.SM2 => false,
            _ => throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
        };
    }

    public static bool IsQuiz(this MO mode)
    {
        return mode switch
        {
            MO.Learn => false,
            MO.Practice => false,
            MO.Quiz => true,
            MO.SM2 => false,
            _ => throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
        };
    }

}

It looks to me like there could be a way to simplify this code but I am not sure how to go about it. Does anyone have any suggestions on what I could do to as every time a new mode is added then now 12-13 lines are going to be added and it's just going to increase to more and more as the switch cases get bigger and bigger.

Would appreciate any advice on how I could fix this problem.

2 Answers2

2

Using your current approach, you have four methods to differentiate between four different enum members. If you add a fifth member you want to handle, you'll have to add another method containing almost identical code, and modify all previously existing methods to handle the new member, if you don't want them to throw.

You don't explain what this code is supposed to do, but it looks like an unmaintainable design. Are you sure enums are the way to go? What is this code really supposed to do?

You could simplify the code somewhat, based on assumptions what it's likely doing:

public static bool IsPractice(this MO mode)
{
    if (!Enum.IsDefined<MO>(mode)) throw new InvalidEnumArgumentException("Unhandled value: " + mode.ToString())
    return mode == MO.Practice;
}

But this could as well be if (mode == MO.Practice) on the call site, eradicating the need for all those seemingly superfluous methods.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

A simple way is

    public static bool IsLearn(this MO mode)
    {
        return mode==MO.Learn;
    }
Anup Sharma
  • 2,053
  • 20
  • 30
  • 2
    Consider what you're advising here: creating a method per enum member, just to check if an enum value ... is equal to an enum value. It's also an extension method, so the calling code will look like `if (mode.IsLearn)`, instead of ... `if (mode == MO.Learn)`. Do you really want to add a method like this per enum member? – CodeCaster Dec 31 '20 at 12:31
  • I see there is a design issue here as you have mentioned in your answer but I think OP just wants to use these methods only to solve his problem. Hence provided a simpler way. – Anup Sharma Dec 31 '20 at 12:37