Below are a few tips on how to write code that is efficient:
- Define types on your variables, constants and functions when possible.
- Always make sure that the script does not run through unnecessary iterations in a loop, or unnecessary statements in an iteration. Use break or continue to terminate or skip to the next iteration.
- If you need to call the f function for the same question more than once in a script, store it in a variable and refer to that variable instead.
- Avoid using calls to user-defined functions in code masks. Set a hidden multi question instead and refer to it with the f function.
- Use methods of the form objects like domainValues and categories when possible instead of hard coding codes(1,2,3,…) in your scripts.
- Use short-circuiting evaluations in expressions involving logical and (&&) and or (||): Place conditions most likely to be true first for the logical or (||) operator, and conditions most likely to be false first for the logical and operator (&&).
- Only use local variables in functions (use the var keyword).
- For Compound questions with large answerlists (eg a Multi using a Loop Reference answerlist), avoid attempting to set all answers at once as it can result in poor respondent experience. Instead reference the compound question individually and only the answers that require updating.