Skip to content

Counting Switch-Case-Default all as one statement? #38

@NW7US

Description

@NW7US

In my C# project, I have a switch statement that has many tens of values based on an Enum. This enum variable is used in a communications environment. Each case of this single switch statement handles, as you know, all of the possible values that enum variable stores.

I have a single method that is nothing but the switch-case-default statement. Each case sets roughly three variables and makes one call to another method which in turn is one of the values used to set one of those three variables.

In my mind, there is NO way to reduce this. The CleanCode extension seems to be counting each case statement, as well as the other statements.

It would seem to me that the counting should only include the 'switch' as a whole. Yes, statements made inside the block of code of a particular 'case' can be analyzed separately. But, seriously, this should not cause the "Method too long...responsibility" hint.

Example:

public enum SomeCoolEnum {

    STATE_VALUE_01 = 0,
    STATE_VALUE_02 = 1,
    ... snip ... for efficiency, I've not included all 63 values ...
    STATE_VALUE_63 = 63
}

public int someReturnValue(SomeCoolEnum theEnumValue) {
    int theReturnValue;
    switch (theEnumValue) {
        case STATE_VALUE_01:
        case STATE_VALUE_02:
        case STATE_VALUE_03:
        case STATE_VALUE_04:
        case STATE_VALUE_05:
            theReturnValue = callSomeSubRoutineToGetTheNewIntBasedOnFirstFive();
            break;
        case STATE_VALUE_06:
        case STATE_VALUE_07:
        case STATE_VALUE_08:
        case STATE_VALUE_09:
            theReturnValue = callSomeSubRoutineToGetTheNewIntBasedOnSecondFour();
            break;
        ... snip ... you get the idea
        default:
            callSomeSubRoutineToDealWithUnknowns();
    }
    return theReturnValue;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions