If-else execution/evaluation

happy new year everybody!

when doing if else like in the image below, what’s inside the if region will only be evaluated when the if condition is met, the else part however seems to be executed regardless (at least according to the quick test I just did…see below). Is there any trick to prevent that? If region plus If Not region looks rather ugly…

image

I’m afraid your observation is correct. we don’t have a dedicated if/else region. the workaround using another if region with a NOT before the condition is indeed current praxis.

More advanced example that might be useful:

image

if_else_delegate.vl (11.3 KB)

@digitalwannabe Also according to the screenshots, I will suggest to look at the Cache region

@joreg understood, thanks!
@yar thanks for the delegate example, it’s nicer to read than the if/not version imo…

afaik there might be a subtle difference between If and delegate, depending on how the compiler translates the code.
the if/else will call create only once (on the random node) while the delegate might be (re)created on invoke, thus recreating the random node (and setting a new random seed)

this can affect performance when running in hot loops.

further reading

3 Likes

good to know, thanks @woei!