1

I'm doing an algorithm in Java using BlueJ as an IDE. I use trackback algorithm to solve the problem and everything works perfectly except for the checkstyle. The checkstyle errors are:

Assignment of parameter i is not allowed [on line 336] 
Assignment of parameter j is not allowed [on line 337]

However, as trackback requires the variables to be passed as the arguments and also be edited according to the situation, so I can't do anything about this.

Does anybody know how to fix this? This is my code (error lines are commented):

 public boolean solve(int i, int j) {
        if (i == 9) {
            i = 0; // line 336
            if (++j == 9) { // line 337
                return true;
        }
        }
        ...
        for (int value = 1; value <= 9; ++value) {
            if (possible[value]) {  
                setCell(i,j,value);  
                if (solve(i+1,j)) {
                    return true;
                }
            }
        }
        game[i][j] = 0;
        return false;
}

1 Answers1

5

You get a warning because Checkstyle considers that modiying method arguments is a bad practice. Either ignore it (or change the checkstyle config) if you don't consider it as bad practice, or change the code to work on copies of the arguments:

public boolean solve(int iIndex, int jIndex) {
    int i = iIndex;
    int j = jIndex;
    // same code as before
}
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • very nice answer :) do you know why it considers that as bad practice? –  May 23 '11 at 11:19
  • 2
    See http://checkstyle.sourceforge.net/config_coding.html#ParameterAssignment for the description of the rule. I personally don't care about assigning new values to parameters. But it can be considered bad practice because you can't tell what the initial values of the parameters were when debugging, for example. It also gives two responsibilities to the parameters : being parameters, and being local variables. – JB Nizet May 23 '11 at 12:24