Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#ffcccc
int array[]; // mayMay be null
int i;       // mayMay be an invalid index for array
if (array != null & i >= 0 & i < array.length & array[i] >= 0) {
  // Use array
} else {
  // Handle error
}

...

This compliant solution mitigates the problem by using &&, which causes the evaluation of the conditional expression to terminate immediately if any of the conditions fail, thereby preventing a runtime exception.:

Code Block
bgColor#ccccff
int array[]; // mayMay be null
int i;       // mayMay be an invalid index for array
if (array != null && i >= 0 && i < array.length && array[i] >= 0) {
  // Handle array
} else {
  // Handle error
}

...

Code Block
bgColor#ccccff
int array[]; // mayMay be null
int i;       // mayMay be a valid index for array
if (array != null) {
  if (i >= 0 && i < array.length) {
    if (array[i] != -1) {
      // Use array
    } else {
      // Handle error
    }
  } else {
    // Handle error
  }
} else {
  // Handle error
}

...

This compliant solution mitigates the problem by using &, which guarantees that both i1 and i2 are incremented regardless of the outcome of the first condition.:

Code Block
bgColor#ccccff
  while (++i1 < array1.length &     // Not &&
         ++i2 < array2.length &&
         array1[i1] == array2[i2])

...