Is there a reason to prefer '&&' over '&' in 'if' statements, other than short-circuiting? Is there a reason to prefer '&&' over '&' in 'if' statements, other than short-circuiting? r r

Is there a reason to prefer '&&' over '&' in 'if' statements, other than short-circuiting?


Short answer: Yes, the different symbol makes the meaning more clear to the reader.

Thanks for this interesting question! If I can summarize, it seems to be a follow-up specifically about this section of my answer to the question you linked,

... you want to use the long forms only when you are certain the vectors are length one. You should be absolutely certain your vectors are only length 1, such as in cases where they are functions that return only length 1 booleans. You want to use the short forms if the vectors are length possibly >1. So if you're not absolutely sure, you should either check first, or use the short form and then use all and any to reduce it to length one for use in control flow statements, like if.

I hear your question (given comments) this way: But & and && will do the same thing if the inputs are length one, so other than short-circuiting, why prefer &&? Perhaps & should be preferred because if they're not length one, if will give me a warning, helping me be even more certain that the inputs are length one.

First, I agree with the comment by @James that you may be "overstating the value of getting a warning"; if it's not length one, the safer thing will be to handle this appropriately, not to just plow ahead. You could make a case that && should throw an error if they're not length one, and perhaps a good case; I don't know the reason why it does what it does. But without going back in time, the best we can do now is to check that the inputs are indeed appropriate for your use.

Given then, that you have checked to make sure your inputs are appropriate, I would still recommend && because it semantically reminds me as the reader that I should be making sure the inputs are scalars (length one). I'm so used to thinking vector-ally that this reminder is helpful to me. It follows the principle that different operations should have different symbols, and for me, a operation that is meant for use on scalars is different enough than a vectorized operation that it warrants a different symbol.

(Not to start a flame war (I hope), but this is also why I prefer <- to =; one for assigning variables, one for setting parameters to functions. Although deep down this is the same thing, it's different enough in practice to make the different symbols helpful to me as a reader.)


No, using && does not offer any advantages other than short-circuiting.

However, short-circuiting is very much preferable for control flow, so much so that it should be the default. if statements should not take vectorised arguments - that's what ifelse is for. If you are passing a logical vector into if typically you would be contracting it to a single logical value using any or all for the evaluation.

The major advantages of short circuiting are in avoiding lengthy or failure-prone steps (eg internet connections - though these should be dealt with through try):

#avoiding lengthy calculationssystem.time(if(FALSE & {Sys.sleep(2);TRUE}) print("Hello"))   user  system elapsed    0.00    0.00    1.99 system.time(if(FALSE && {Sys.sleep(2);TRUE}) print("Hello"))   user  system elapsed       0       0       0 #avoiding errorsif(FALSE & {stop("Connection Failed");TRUE}) print("Success") else print("Condition not met")Error: Connection Failedif(FALSE && {stop("Connection Failed");TRUE}) print("Success") else print("Condition not met")[1] "Condition not met"

It is clear that in order to take advantage of these features, you would have to know in advance which steps take the longest or are prone to errors and construct the logical statement appropriately.