Prevent slow calculate conditions from firing when the form is opened

commcare

I have a lot of calculate conditions in my form in hidden values, but they are only necessary to evaluate if the question "food_not_in_list" = 'yes'. These calculates are slowing down my form because they fire when it's opened. Can I either:

  1. Add display condition to those hidden values so they won't calculate?

  2. Wrap them in an if statement that checks the condition … e.g. Calculate condition = if(food_not_in_list != 'yes', '', do-the-big-calculate)

Best Answer

Unfortunately the hairy part about declarative logic is that it can be a bit tough to decide when processing happens!

To answer your questions:

  • A hidden value's display condition being false does not prevent the hidden value's calculation from executing. All calculations are performed regardless of whether their display conditions are met.

  • Inside of a calculation (or display condition, etc...) if() statements on the other hand do "short circuit logic", meaning that the branch that isn't used won't be executed at all.

    • This is also true of the logic operators and and or

Example:

There is a hidden value with the calculate expression: long_expensive_calc()

Setting the expression instead to

if( #form/time_to_go = 'yes' , '', long_expensive_calc())

will prevent the "expensive" portion from running until the time_to_go is set to yes.