1. Understand getTag() and setTag() overhead
2. Set the script triggering time wisely
Depending on what each script does, the script execution time may vary from a few milliseconds to a few seconds. If the execution time is longer than the triggering time interval, the script will not be executed on each interval exactly and each execution interval will be longer. It is advisable to set the interval to be one second or longer, unless you have high performance CPU or short scripts.
5. Limit the number of scripts in a project
6. Avoid unnecessary scripts if possible
I/O tags (e.g. from sensors or PLC devices) can be used to trigger alarms directly. If possible, avoid using I/O tags to trigger scripts to change the values of virtual tags, and then using the virtual tags to trigger alarms. Consider using the tag output expression to scale the tag value and use the scaled value to trigger an alarm. This will reduce the number of virtual tags and scripts.
7. Prefer local variables to global variables
8. Avoid infinite loops at all costs
Make sure your loops contain proper terminating conditions. Without a terminating condition, a loop will repeat forever. This is also known as an infinite loop. If it happens, the script will not finish executing. This may hold IntegraXor Server to proceed to next task while waiting for scripting execution to end.
9. Keep functions short and simple
There is no rule on the maximum number of lines for a function. A general rule of thumb is that a function should do only one thing.This practice may be hard to comply because you may end up writing many short functions, but in long term the benefits will outweigh the trouble to write many short functions. Short functions are easier to understand and maintain. They also encourage code reuse.
Share with us if you have your own tips as well!