PDA

View Full Version : Global Variables vs Data Duplicates


scc
07-24-2008, 11:52 AM
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 :)

jsebrech
07-28-2008, 03:19 PM
Store it as a static variable, with static accessor methods. Available globally, calculated once, but not actually a global variable, so it doesn't pollute the global namespace.

swivelmaster
08-26-2008, 08:19 PM
Agreed with this.

My question would be, are all these objects resizing each other in relation to the full size of the window, or in relation to the size of some smaller container they're in?

If they're in a smaller container, than each one only needs to know how big its parent in.

But yeah, static is good. In Flex, you can get the values via Application.application.stageWidth and Application.application.stageHeight. Same concept.