Consider the following loop (in Swift - but my question is language independent):
var index = standardizedTimeSpans.count - 1
while index >= 0 && timeSpan < standardizedTimeSpans[index] {
index--
}
index++
In the above code, the standardizedTimeSpans variable is an array of Doubles and the timeSpan variable is a Double.
The above loop may decrement the value of index down to a maximum of -1. If my conditional expression for the while statement was ordered the other way around (i.e. timeSpan < standardizedTimeSpans[index] && index >= 0
), a runtime error would occur with array out of bounds in the event of the index reaching -1.
My question:
Is this code considered "safe"? In other words, is there a risk of a compiler change that doesn't necessarily sequence the evaluation of conditional from left to right? Of course, the alternative is to evaluate the conditions separately and but a break expression in the loop.