Versions Compared

Key

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

...

Wiki Markup
This noncompliant code example, derived from Flanagan \[[Flanagan 2005|AA. Bibliography#Flanagan 05]\], has two variables, with no guarantees regarding their current values. The code must validate its data and then check whether {{array\[i\]}} is nonnegative.

Code Block
bgColor#ffcccc
int array[]; // may be null
int i;       // may be a valid index for array
if (array != null &
    i >= 0 & i < array.length &
    array[i] >= 0) {
  // handle array
} else {
  // handle error
}

Wiki Markup
This code can fail due to the very errors it is attempting to prevent. When {{array}} is null or when {{i}} is not a valid index, the reference to {{array\[i\]}} will cause a {{NullPointerException}} or an {{ArrayIndexOutOfBoundsException}} to be thrown. This happens because the {{&}} operator fails to prevent evaluation of its right operand, even when evaluation of its left operand proves that the right operand is invalid.

Compliant Solution (Use &&)

...