Try to avoid DYNAMIC calls

Share this topic:



Link to this posting

Postby Ursego » 19 Feb 2013, 22:27

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")).
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33

Return to Elegant Code

Who is online

Users browsing this forum: No registered users and 2 guests


Try to avoid DYNAMIC calls

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





free counters

eXTReMe Tracker