Similar methods using loops

Is it recommended to combine two similar methods into one?

public void initialize_board(){
    for(int y = 0; y < board.length; y++){
        for(int x = 0; x < board.length; x++){
            if(x == 0)
                board[x][y] = (char)y;
            else
                board[x][y] = '~';
        }
    }
}

public void display_board(){
    for(int y = 0; y < board.length; y++){
        for(int x = 0; x < board.length; x++){
            System.out.print(board[x][y]);
        }
        System.out.print("\n");
    }
}

Answer

By the Single Responsibility Principle, they should remain separate methods. Furthermore, initialize_board() should probably be a constructor for a Board class, and display_board() should be its toString().

Attribution
Source : Link , Question Author : user89428 , Answer Author : 200_success

Leave a Comment