Feuermagier / autograder

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

Suggest variable for conditionally assigned expressions #473

Open Luro02 opened 3 months ago

Luro02 commented 3 months ago

Description

Similarly to #325, but for writes, when we have for example

if (a) {
  array[0] = some complex expression;
} else if (b) {
  array[0] = some other complex expression;
} else if (c) {
  array[0] = some other complex expression;
} else {
  array[0] = some other complex expression;
}

It makes sense to introduce a variable that is assigned to in the conditions, so that when one changes the target (from array[0] to for example a method call), one must not change many lines.

Motivation:

int index = cell.getMemoryPosition();

if (isNextAiCommand(nextAi, index)) {
  memoryArray[index] = nextAiSymbol;
} else if (isNextCommandOfAnyAi(nextAis, index)) {
  memoryArray[index] = otherAisSymbol;
} else if (isAiBomb(cell)) {
  memoryArray[index] = cell.getCommand().getOwnerAi().orElseThrow().getAiBombSymbol();
} else if (isAiCommandFromAi(cell)) {
  memoryArray[index] = cell.getCommand().getOwnerAi().orElseThrow().getAiSymbol();
} else {
  memoryArray[index] = unchangedSymbol;
}