The problem

Today while refactoring some global static classes code to be more manageable at work thought of one thing. Thing is that those classes stuff often should get initialized. Where initialization it is usually called? Well who knows, anywhere where it fits… With big project where you have many global managers like money manager, hints manager, events manager etc etc their initializers are usually hidden somewhere while they are used in multiple places. Now often it happens that something changes and needs to access that class sooner then it was needed before. What follow is probably null reference error or something similar. And now you need to go, figure out where it is initialized now, what needs to be done to initialize it sooner. Or just try to move code that needs it to some later stage… Sounds bad right? Some spaghetti code stuff in project where multiple programmers work on same code base. Especially when stuff in those global classes needs to be called in various places that can come at random order depending on server and user input.

Static initializers in AS3

So refactoring stuff I was thinking about this problem again. Interesting thing about Flash is that it uses lazy static class initialization. In short that means that static class is not initialized until it is referenced somewhere in code. Neat feature though still a compromise. We get start up performance as not all classes are initialized from the start but then we loose some as each time class is referenced there is a check for it being initialized(I guess, that’s how it usually is done). Now that’s good but what if we need not only to initialize variable but run some code. For example some parsing of variables content. Google for the rescue: Static initializers. Nice, short and clear article.

 

{

//put code that will be called when class if referenced for first time

}

 

There is one thing though. You can debug code inside those brackets. So I instead just put a call for some static function which I can debug then.

So now I have something like that:

 

public class GlobalMoneyManager

{

public static var PricesXML:XML;//set when loaded

{

Init();//initialization code called

}

public static function Init():void

{

//parsing prices, calling stuff, etc

}

}

 

 

 

Now then it works for me like that.

  1. Site/Game loads and XML with info is loaded.
  2. Then there are like 10 UIs that use information that needs to be retrieved from that XML but none of  them is shown at the start
  3. When user action results in one of those UIs showing up, UI is initialize and reference something in GlobalMoneyManager
  4. GlobalMoneyManager is initialized
  5. Static initializer code is called
  6. Init is called and class if filled with parsed info
  7. Control is returned to UI and data is accessed

The end

Works like magic. I wonder if there is a catch to this that would make it a bad coding style that raises more problems then it solves. So far I do not see any.