inBounds

What does “in bounds” mean?

In sports played on a court or field like tennis, basketball, or football, what does it mean to be “in bounds”?

The bounds of an array are the limits of the valid indexes into that array.

For a 1d array, if the length of the array is \(len\), then the valid indices are the \(i\) in the range \(0 \le i \lt len\).

For a 2d array we have two dimensions, so two indices and two ranges.

If \(rows\) is the number of rows and \(cols\) is the number of columns then the indices \(r\) and \(c\) need to be in the ranges \(0 \le r \lt rows\) and \(0 \le c \lt cols\).

One way to write inBounds

public boolean inBounds(int[][] g, int r, int c) {
  if (g.length - 1 >= r) {
    if (0 <= r) {
      if (c <= g[0].length - 1) {
        if (c >= 0) {
          return true;
        }
      }
    }
  }
  return false;
}

This works.

But it’s kind of hairy.

Combine some expressions with &&. .

public boolean inBounds(int[][] g, int r, int c) {
  if (g.length - 1 >= r && 0 <= r) {
    if (c <= g[0].length - 1 && c >= 0) {
      return true;
    }
  }
  return false;
}

This is at least a bit shorter and less deeply nested.

Simplify by removing some arithmetic.

public boolean inBounds(int[][] g, int r, int c) {
  if (g.length > r && 0 <= r) {
    if (c < g[0].length && c >= 0) {
      return true;
    }
  }
  return false;
}

By changing >= and <= to > and < we don’t have to subtract one from each length.

Put in math order: \(low \le middle \lt high\).

public boolean inBounds(int[][] g, int r, int c) {
  if (0 <= r && r < g.length) {
    if (0 <= c && c < g[0].length) {
      return true;
    }
  }
  return false;
}

We can’t write 0 <= r < array.length the way we could in math, but if you squint and treat r && r like it is just one r, it’s pretty close.

Combine the two if statements.

public boolean inBounds(int[][] g, int r, int c) {
  if (0 <= r && r < g.length && 0 <= c && c < g[0].length) {
    return true;
  }
  return false;
}

This is really just a step on the way to the next version.

We don’t need an if at all!

public boolean inBounds(int[][] g, int r, int c) {
  return 0 <= r && r < g.length && 0 <= c && c < g[0].length;
}

The condition of an if is already a boolean. Just return it.

How it started.

public boolean inBounds(int[][] g, int r, int c) {
  if (g.length - 1 >= r) {
    if (0 <= r) {
      if (c <= g[0].length - 1) {
        if (c >= 0) {
          return true;
        }
      }
    }
  }
  return false;
}

How it’s going.

public boolean inBounds(int[][] g, int r, int c) {
  return 0 <= r && r < g.length && 0 <= c && c < g[0].length;
}

The good version uses several idioms that you should get used to:

  • Ordering comparisons to express ranges
  • Using short circuiting behavior of &&
  • Returning a boolean expression directly

The more you use consistent idioms, especially idioms other programmers use, the easier it will be to read your code.

Why inBounds?

This method is extremely useful in dealing with 2d arrays.

When dealing with neighboring cells in a 2d array some cells don’t have the normal eight neighbors.

Instead of dealing with these literal edge and corner cases, we can write code that just checks than any array indexes we try to use are in bounds before we try to use them.