Feuermagier / autograder

Automatic grading of student's Java code
MIT License
13 stars 7 forks source link

Instead of a switch-case, the enum should have a method with the given code #497

Open Luro02 opened 5 months ago

Luro02 commented 5 months ago

What it does

This is an extension of #493, but for any code on the right side of a case.

If one has a switch over an enum and more than x lines of code, the lint should suggest refactoring the code, so that those are methods in the enum.

Lint Name

No response

Category

No response

Example

switch (args[1]) {
    case MemoryInitMode.STOP_NAME - > {
        if (args.length != 1 + REQUIRED_ARGUMENT_COUNT) {
            return new Result(ResultType.FAILURE, WRONG_ARGUMENT_NUMBER.formatted(1));
        }
        builder.setInitMode(MemoryInitMode.STOP);
    }
    case MemoryInitMode.RANDOM_NAME - > {
        if (args.length != 1 + REQUIRED_ARGUMENT_COUNT + OPTIONAL_ARGUMENT_COUNT) {
            return new Result(ResultType.FAILURE, NO_SEED_GIVEN);
        }
        long seed;
        try {
            seed = Long.parseLong(args[SEED_POSITION]);
        } catch (NumberFormatException e) {
            return new Result(ResultType.FAILURE, SEED_IS_NO_INTEGER);
        }
        if (seed > MAXIMUM_SEED_SIZE || seed < MINIMUM_SEED_SIZE) {
            return new Result(ResultType.FAILURE, SEED_RANGE);
        }
        builder.setInitMode(MemoryInitMode.RANDOM);builder.setInitModeSeed(seed);
    }
    default - > {
        return new Result(ResultType.FAILURE, NOT_A_MEMORY_INIT_MODE.formatted(args[1]));
    }
}

Could be written as:

<code>