0

There are a couple of examples from a simple game based on two players putting X and O on 2d array

First method should return true if element filed[y][x] is ok;

public boolean yxInField(int y, int x, char[][] field) {
    return field != null
            && y >= 0
            && x >= 0
            && y < field.length
            && field[y] != null
            && x < field[y].length;
}

Second should return true if at least one of calls of check win condition for a specific direction returned true

public boolean checkWin(int y, int x, char[][] field) {
    return
            //E-W
            checkDirection(y, x, 0, -1, field)
            //S-N
            || checkDirection(y, x, -1, 0, field)
            //SE-NW
            || checkDirection(y, x, -1, -1, field)
            //SW-NE
            || checkDirection(y, x, -1, 1, field);
}

But i'm not certain if this style is ok. If not what would be the better way to do those methods?

1 Answers1

1

Yes, that is perfectly OK. Distributing the complex condition over multiple lines and interleaving the parts with useful comments is even better.

The only thing to consider (if performance is any of a concern) is the order of the sub-conditions. In you have multiple ORs concatenated, you want the ones most likely to succeed to go first so the evaluation can be cut short most of the times. With ANDs you want the ones most likely to fail to go first for the same reason.

Martin Maat
  • 18,218
  • 3
  • 30
  • 57