4 Replies Latest reply on Jul 26, 2008 7:03 AM by Stu_Cox

    Global Variables vs Data Duplicates

      I haven't done a massive amount of OO programming, but I know the basics. I've found myself using more and more AS in my Flash designs to the point where they're practically 100% AS (split quite tidily into classes etc).

      Where I fall short is a lot of the best practices... I know that global variables are to be avoided wherever possible and that passing variables to classes and functions is the way to go instead, but I've also read many times that having multiple copies of data in different places is a bad idea, which also makes perfect sense to me but these seem to conflict in certain situations.

      The problem I have with this is best illustrated with a current design issue I'm scratching my head over:

      I've got a flexible design which resizes to the window size, but I've set a maximum size (800 x 600), so there's essentially a frame within the flash design which everything is contained within - this will resize to fit the window if it's less than 800 x 600 or stay at 800 x 600 if the window's bigger than that.

      A lot of the components in my design rely on knowing the current frame size (for positioning etc), which is obviously calculated from Stage.width & Stage.height. The 3 options I can think of for handling this frame size variable are:

      1. Have a _global variable: bad practice, particularly if this movie is at some point embedded into another movie as it may conflict.

      2. Have a variable in a separate or top-level class: a bit of a referencing nightmare, every component has to be passed either a reference to this class or the variable itself (which goes against my interpretation of encapsulation) - if the latter, then problems occur as the components don't just need the variable at the moment when the window is resized (as certain animations rely on the mouse position relative to this frame) so the variable would have to be stored locally in each component = data duplication = bad practice.

      3. Calcuate the variable locally: would have to be performed every time the window is resized or whenever it's needed, not very efficient and also means data duplication = bad practice again.

      What would you do here? Is there another technique I haven't spotted? What would be considered the best programming practice?

      Thanks :)
        • 1. Re: Global Variables vs Data Duplicates
          Level 7
          I'd use a singleton. It's kind of like a global class. Yes, everywhere that
          you need the data in the object, you'll need to get a reference to it, but
          otherwise it's a perfect solution.

          If you're not familiar with singleton, it's like so in AS2:

          class com.blurredistinction.Utility{

          private static var instance:Utility;

          private function Utility(){}

          public static function getInstance():Utility
          if(instance == undefined){
          instance = new Utility();
          return instance;

          //getter/setter methods....


          You have a main movie that gets the first instance, and sets data using the
          getter methods.

          var utility = com.blurredistinction.Utility.getInstance();

          Any other place you need the data, you get an instance, and use the getter
          methods. Because it's a singleton there's just one instance ever... so the
          data you set in movieA is available in movieB, movieC, etc.

          I use singleton a lot for things like this.

          Dave -
          Head Developer
          Adobe Community Expert

          • 2. Re: Global Variables vs Data Duplicates
            Stu_Cox Level 1
            Thanks for the response, great suggestion.

            Yeah I read about the singleton model the other day actually, although it was in the context of a full design pattern rather than just a tool. I didn't think to apply it to this in such a strict way - my idea for the separate class would have essentially followed singleton ideas in that there would only be one instance, but of course if I'm going to do that I might as well go the whole way and do it properly as you say and ensure there's only ever one instance.

            Another similar approach I considered was storing the variable as a static variable in a separate class and never actually instancing the class - just accessing it from anywhere via ClassName.variableName.

            As far as I can see, this would have a similar effect to the singleton approach but would avoid having to pass references - is this correct? Although having said that, surely one of the great thing about the singleton is I wouldn't need to pass references as such, I'd just need to "try" to instance the class whenever I need it and it would give me a reference?

            Are there any other differences between these two approaches, or more specifically any benefits the singleton approach would give over an instanceless class with static variables in this example?

            Thanks again for taking the time to reply.
            • 3. Re: Global Variables vs Data Duplicates
              Level 7
              A static class would work ok, but many argue they break the rules of oop
              since they are really just namespaces for a group of functions. I've used
              both, now I prefer the singleton. But it's up to you... whichever works is

              >>I'd just need to "try" to instance the class whenever I need it and it
              >>would give me a reference?

              Sort of, you simply 'get' an instance when you need the data/methods in the
              object. Because of how its coded you only get the 'one' instance. That's why
              it's nice for passing data around.

              Dave -
              Head Developer
              Adobe Community Expert

              • 4. Re: Global Variables vs Data Duplicates
                Stu_Cox Level 1
                Great description, thanks for your help.