Java Reference
In-Depth Information
i < array.length && array[i] >= 0) {
// Handle array
} else {
// Handle error
}
Compliant Solution (Nested if Statements)
This compliant solution uses multiple if statements to achieve the proper effect.
Click here to view code image
int array[]; // May be null
int i; // May be a valid index for array
if (array != null) {
if (i >= 0 && i < array.length) {
if (array[i] >= 0) {
// Use array
} else {
// Handle error
}
} else {
// Handle error
}
} else {
// Handle error
}
Althoughcorrect,thissolutionismoreverboseandcouldbemoredifficulttomaintain.
Nevertheless, this solution is preferable when the error-handling code for each potential
failure condition is different.
Noncompliant Code Example (Improper && )
This noncompliant code example demonstrates code that compares two arrays for ranges
of members that match. Here i1 and i2 are valid array indices in array1 and array2 , re-
spectively. Variables end1 and end2 are the indices of the ends of the matching ranges in
the two arrays.
Click here to view code image
if (end1 >= 0 & i2 >= 0) {
int begin1 = i1;
Search WWH ::




Custom Search