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.
monteCarlomethod is problematic: it modifies a global variable not passed as an argument, which may be puzzling to readers of your code. Better to assign tomatrixoutside the method. – Andres F. Feb 25 '15 at 15:15monteCarlomethod has been updated. please take a look. – Muztaba Hasanat Feb 25 '15 at 15:29monteCarloknow that the method modifies a global variable? – Andres F. Feb 25 '15 at 16:05