2

This question already has an answer here:

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.

marked as duplicate by Doc Brown, Telastyn, Community Jul 24 '15 at 0:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I believe that if the suggestion of language independence were edited out of this question, it would no longer be a duplicate as it would then be specifically about whether this is a good idea in swift, which may or may not be the same as other languages... – Jules Jul 23 '15 at 23:43
0

I'm not sure about Swift specifically, but in general using the && operator short circuits and ends early if the left side is false. The code after the && will not be run.

Short circuiting is common practice in many languages.

  • That is the case in swift, however, I'm wondering if is this a good practice (a safe assumption) in general? – Dragonspell Jul 23 '15 at 21:20
  • 1
    Yes, short circuiting is an accepted practice – Dan Ambrogio Jul 23 '15 at 21:22
  • 1
    That does not answer the question about "risk of a compiler change" in future - it just repeats what the OP already knows, that in the current version of Swift (and other languages) short circuit evaluation works. – Doc Brown Jul 23 '15 at 21:39
2

"but my question is language independent"

It is of course language dependent. In any programming language where short circuit behaviour is part of the language definition, the code is "safe". Otherwise it is not.

Fortunately, any serious programming language of the C-based language families I know of has short circuit behaviour "by definition" (and lots of other programming languages, too). I do not know Swift, but since the inventors of that language were professionals, I am pretty sure they care for backwards compatibility. So I would not expect the behaviour to be changed in that language in the future.

  • If some crazy compiler change reversed the order of evaluation, as the OP hypothesizes, then not using short-circuit operator doesn't really guarantee safety, or backwards compatibility, anyway. Even with eager evaluation, order of evaluation can still matter, if evaluating causes side effects. The question is based on a wrong premise. – Konrad Morawski Jul 23 '15 at 21:51
  • I vaguely remember Pascal where in implementation was allowed to, but not required to perform short circuit evaluation for AND and OR. That was the worst case possible. – gnasher729 Jul 23 '15 at 22:52
0

my question is language independent

No, it isn't.

You are assuming that

  • the order of evaluation is well-defined
  • the order of evaluation is left-to-right
  • && is lazy in its right operand

All three of which are language-dependent. For example, in Visual Basic, And is strict in both its operands.

  • Good point. I guess I wasn't thorough as I should have been when I wrote that. Indeed, my question was for languages that satisfy those assumptions without explicitly stating them. – Dragonspell Jul 24 '15 at 0:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.