I have 4 nested loops that used based on the case given. The code is quite similar but there is a little difference in the comparison. Please check this code:
If Case 1:
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i][j+1])
// do something
}
}
If Case 2:
for (i = 0; i < length; i++) {
for (j = 4; j > 0; j--) {
if (array[i][j] == array[i][j-1])
// do something
}
}
If Case 3:
for (i = 0; i < length; i++) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i+1][j])
// do something
}
}
If Case 4:
for (i = 4; i > 0; i--) {
for (j = 0; j < length; j++) {
if (array[i][j] == array[i-1][j])
// do something
}
}
As you can see that the decrement and increment are different too. It is quite hard for me to figure out how to refactor this. They have similar code behavior (duplication) but they have different conditions in the for
and if
statement on each case.
Aucun commentaire:
Enregistrer un commentaire