Writing clean code…. (No globals and no _root)

Hi gang,

While I know it can be difficult; I have always found it best to recommend that programs can and should be written without the use of global variables and without targeting _root. While both techniques are valid and do have their place, the convenience of using them tends to encourage programmers not to place their code in a centralized location or to be smart about their use of memory.

In the case of globals, developers get the convenience of not having to remember the path to their objects or variables and thus can use them freely. This can make it very difficult to debug your code when something goes wrong. Several functions reading and writing from a global variable can often lead to an unexpected overwrites of data in your variable and it can be a nightmare trying to track down exactly the sequence of events that cause the problem.

Furthermore I can’t begin to tell you the number of support cases we get where a user has of created fairly large data structure stored in global objects or arrays not realizing that to make anything global is to disable the automatic garbage collection (of that object or variable) the player does to keep memory being used efficiently. After a few seconds you can literally see the usage of RAM building on the system followed by a dramatic performance drop because the explicit “delete� statement was not ever used to free up the memory of the objects that were no longer needed.

Use of _root tends to encourage programmers to scatter code in various child movie clips and on handler events. This can make tracking down a problem just as difficult as when you use globals. Also, at some point there tends to be some reason that SWF must be loaded into another. Storing data in _root means you increase the chance of overwriting data stored in the root movie or creating variables in the root that do not get removed when the child SWF has been unloaded or replaced. This leads to a reduction in the available memory in much the same way overuse of global variables does.

So what should you do? Well just avoid the use of global variables and _root. You can always store you data in a specific movie clips (or on the frames of the main timeline) and you can always use relative paths to target those movie clips or whichever timeline contains the code you need to target. Forcing yourself to follow these rules will help you get better and understanding scope and will encourage you to think about structuring your application before you write it. This will help you to keep your code a bit more organized, and eventually will lead to less bugs. Over all it will make it much easer to troubleshoot bugs when you get them.

Questions, comments, let me know.


3 Responses to Writing clean code…. (No globals and no _root)

  1. Usually i put a:var root = this;on my AS layer: that saves me a lot of thinking about relative paths; everywhere I need to go back to ‘_root’, i can now just use ‘root’…

  2. Alexandre Madurell says:

    I created a very basic class (.as) where to store a language variable, with a couple of simple getter and setter functions.Then I instantiate it in the early frames of the main function and let the user choose which language he prefers or set one by default.I guess that approach keeps using memory anyways unless you use a “kill” method for the class, right?Any comments will be appreciated ;)And congrats for your weblog

  3. Ken says:

    Hi Alexandre,You may consider storing data you want to keep persistent in a Static variable so that only one copy per class exists in memory regardless of how many instances of the class are instantiated. If you don’t need the variable any more you can set it to null or use the delete operation. Outside of that, I am glad you are using custom classes. They are essential for good application architecture.Thank for the post.-Ken