Page 1 of 1

Try to avoid DYNAMIC calls

PostPosted: 19 Feb 2013, 22:27
by Ursego
Don't call functions and events dynamically. Instead, cast to the needed data type (copy the pointer to a variable of the class the called function/event is defined in) and perform a STATIC call.

Usually, that approach makes the code a little bit longer (sometimes requiring a whole choose case block instead of a single line of code), but the advantages, listed below, are more important:

1. Type-safety. A non-existing script will never be called after possible changes in the application in the future. Instead, a compilation error will force developers to find a solution. Solving the problem by checking ClassName() is not very elegant.

2. Clarity to code readers. Developers immediately see the class where the called function exists. In DYNAMIC calls they have to guess or investigate.

Remember that TriggerEvent() and PostEvent() functions perform a DYNAMIC call when the event name is passed to them as a string (not as a value of TrigEvent enumerated data type like Clicked!), so use the dot-notation syntax instead - like dw_main.event ue_after_open() instead of dw_main.TriggerEvent("ue_after_open")).