0

I am writing a class in java of Monte-Carlo algorithm. Here is the written code -

public class MonteCarlo {
    int[][] matrix;
    public void monteCarlo(List<Node> nodeList) {
        matrix = new int[nodeList.size()][nodeList.size()];
        initMatrix(nodeList, matrix);
    }

    private void initMatrix(List<Node> list, int[][] matrix) {

    }
}

public class TestMain {
        public static void main(String[] args) throws IOException{

        GraphGenerator generator = new GraphGenerator();
        List<Node> nodeList = generator.graphGenerator(50, 11000, 6000, 40);
        MonteCarlo monteCarlo = new MonteCarlo();
        monteCarlo.monteCarlo(nodeList);
   }

}

In here initMatrix method i am passing two parameter. But the matrix array is globally declared. I personally sometime found that is helpful to keep track which method is responsible for what. But is it a bad coding practice to pass a variable parameter even it is declared in globally.

Muztaba Hasanat
  • 305
  • 1
  • 4
  • 12
  • see also: [Passing member variable as a method parameter](http://programmers.stackexchange.com/questions/146508/passing-member-variable-as-a-method-parameter) – gnat Feb 25 '15 at 15:14
  • Your `monteCarlo` method is problematic: it modifies a global variable not passed as an argument, which may be puzzling to readers of your code. Better to assign to `matrix` *outside* the method. – Andres F. Feb 25 '15 at 15:15
  • 1
    Mods: doesn't this kind of question belong in Code Review? – Andres F. Feb 25 '15 at 15:16
  • 1
    This is not appropriate for CR.SE because it is not a complete, working program or module. It is simply a code skeleton. –  Feb 25 '15 at 15:23
  • @AndresF. `monteCarlo` method has been updated. please take a look. – Muztaba Hasanat Feb 25 '15 at 15:29
  • Muztaba, that's not enough. How would someone reading the signature of `monteCarlo` know that the method modifies a global variable? – Andres F. Feb 25 '15 at 16:05

0 Answers0