Feuermagier / autograder

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

Suggest replacing a mapping with iterating over `.values()` or a constant `Map`. #531

Open Luro02 opened 1 month ago

Luro02 commented 1 month ago

What it does

It makes the code smarter. Note: This is difficult to notice, wouldn't make sense to subtract for this.

Lint Name

No response

Category

complexity

Example

return switch (string) {
    case "A" -> MyEnum.A,
    case "B" -> MyEnum.B,
    case "C" -> MyEnum.C,
    case "D" -> MyEnum.D,
    default -> throw new IllegalArgumentException("invalid input string: " + string);
};

Could be written as:

for (var value : MyEnum.values()) {
  if (string.equals(value.toString())) {
    return value;
  }
}

throw new IllegalArgumentException("invalid input string: " + string);