Replacing IFs with ALLs and IF-NOTs with ANYs


#1

I’ve known that there were cases where people used ALL to run a chain of code without using the result. So where other languages might write:

if (condition1 and (cond = ition2) and con(dit, ion3)))
    break;

Rebol programmers might say:

all [
    condition1
    cond = ition2
    con dit ion3
    break
]

With conditional AND in Ren-C, you get a choice. It seems to me that whether it’s clearer or not is on a case by case basis.

I saw something in some Red code that was the other way around. Imagine a negated if, which you can write in Ren-C like this if you like:

 if not condition1 and [cond != ition2] and [not con dit ion3] [
     break
]

But you could also use an ANY for this, with no IF:

 any [
     condition1
     cond = ition2
     con dit ion3
     break
 ]

Maybe I’m late to the game in thinking of using ANY in this way, to test things when you’re really not intending to use the result, just to conditionally avoid running some code in later any cases. But it brings up the cognitive question of whether you think of your code like this or not. It’s hard to say with a fake example like this.

In historical Rebol, if you wanted to put multiple lines in the last case of an any you could do that with a GROUP!, e.g.

 any [
     condition1
     cond = ition2
     con dit ion3
     (
         line one
         line two
     )
 ]

This lets you stop worrying about whether the lines of code at the end are able to affect the ANY’s result Ren-C gives you ELSE and THEN, so for the ANY case:

 any [
     condition1
     cond = ition2
     con dit ion3
 ] else [
     line one
     line two
 ]

But you can use THEN or ELSE with either construct, as appropriate (or you can use both). I’ve found that:

all [...] then [...]

…is particularly pleasing and comes up often, and is better than if all [...] [...] for keeping your place in the source when the ALL block is long. It scans better.