Feuermagier / autograder

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

Suggest introducing an attribute for enum values #493

Closed Luro02 closed 2 months ago

Luro02 commented 2 months ago

What it does

In Java, one can not only have a simple enumeration like

enum Color {
  RED,
  GREEN,
  BLUE;
}

but also have attributes:

enum Color {
  RED(0xff0000),
  GREEN(0x00ff00),
  BLUE(0x0000ff);

  private int value;

  Color(int value) {
    this.value = value;
  }
}

Sometimes this is not used, like here:

private static int toNumber(Color color) {
    return switch (color) {
      case RED -> 0xff0000;
      case GREEN -> 0x00ff00;
      case BLUE -> 0x0000ff;
    };
}

which is unnecessary, because the constant values on the right could be attributes.

The same is for the inverse where the constant is on the left and the enum on the right.

Note: Those cases should be ignored by the ClosedSetShouldBeEnum check.

Alternatively one could suggest a constant map in the form Map<Enum, Constant> or Map<Constant, Enum>.

Lint Name

CONSTANT_SHOULD_BE_ENUM_ATTRIBUTE

Category

oop

Example

For example see above.

private static final int MOV_R_CASE = 1;
private static final int MOV_I_CASE = 2;
private static final int ADD_CASE = 3;
private static final int ADD_R_CASE = 4;
private static final int JMP_CASE = 5;
private static final int JMZ_CASE = 6;
private static final int CMP_CASE = 7;
private static final int SWAP_CASE = 8;
private static final int TOTAL_COMMANDS = 9;
[...]
return switch (pId) {
                case MOV_R_CASE -> AiCommandType.MOV_R;
                case MOV_I_CASE -> AiCommandType.MOV_I;
                case ADD_CASE -> AiCommandType.ADD;
                case ADD_R_CASE -> AiCommandType.ADD_R;
                case JMP_CASE -> AiCommandType.JMP;
                case JMZ_CASE -> AiCommandType.JMZ;
                case CMP_CASE -> AiCommandType.CMP;
                case SWAP_CASE -> AiCommandType.SWAP;
                default -> AiCommandType.STOP;
};