Sign in to follow this  

Data Persistence - Global Variables & Singletons

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been trying to become a better programmer, i've always heard that global variables are bad yet i have insisted on using them. In my current project i am making a fluid dynamics simulator, thus i have a few large datasets with 50k-100k objects each. The project is made in C# XNA. In each frame(Update) there are a lot of functions that manipulate the datasets. "Draw" then calculates the colordata and outputs it to the screen. Currently the datasets consist of global arrays that the functions directly interact with. I got the suggestion to make the datasets into a singleton class to avoid globals, problem is that i see myself making local copies of the datasets, thus making it a singleton seems counterproductive. In the heated threads on this forum there is a repeating notion that experienced programmers don't need to use global variables or singletons. So my question is this, how am i supposed to make the datasets persistent if i can't declare the datasets in a higher scope? As an example, the sound manager has no need to know about the fluid datasets but unless the data is stored globally i'm going to lose all the data the instant the program enters the soundmanager. Magic?

Share this post


Link to post
Share on other sites
Quote:

As an example, the sound manager has no need to know about the fluid datasets but unless the data is stored globally i'm going to lose all the data the instant the program enters the soundmanager.


Just because the sound manager doesn't have access to the data doesn't mean the data is purged from memory...

Share this post


Link to post
Share on other sites
Move your global data sets into members of one of your high-level classes (possibly your very top level "App" class or equivilent). Pass them down by reference or pointer to the instances and methods that need them. That's it, there's no real magic to it.

Share this post


Link to post
Share on other sites
The source looks kinda like this...


namespace FluiD
{
public class Game1 : Microsoft.Xna.Framework.Game
{
fluidData fd = new fluidData();
//fd is an instant of the fluidData class that contains the arrays.

//Update is called from the background (I don't call it)
protected override void Update(GameTime gameTime)
{
//Update contains several functions that directly manipulate the datasets(fd)
}

//Draw is automatically called.
protected override void Draw(GameTime gameTime)
{
//Draw outputs the dataset(fd)
}

}



Quote:
Just because the sound manager doesn't have access to the data doesn't mean the data is purged from memory...


If the datasets aren't declared one level up from Update, the data is purged. Once the soundmanager ends the update also closes until the next cycle.

Quote:
Move your global data sets into members of one of your high-level classes (possibly your very top level "App" class or equivilent). Pass them down by reference or pointer to the instances and methods that need them. That's it, there's no real magic to it.


How? I have no way of passing a reference to "Update" and even if i did i need the data back (and altered) for the next cycle. If the functions are to alter the datasets i need either, a global variable, or a singleton.

I'm sorry if i come off as aggressive but i get the feeling that somewhere along the line i missed some important part of the puzzle.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crimsonus
The source looks kinda like this...


namespace FluiD
{
public class Game1 : Microsoft.Xna.Framework.Game
{
fluidData fd = new fluidData();
//fd is an instant of the fluidData class that contains the arrays.

//Update is called from the background (I don't call it)
protected override void Update(GameTime gameTime)
{
//Update contains several functions that directly manipulate the datasets(fd)
}

//Draw is automatically called.
protected override void Draw(GameTime gameTime)
{
//Draw outputs the dataset(fd)
}

}


You are declaring fluidData fd as a member variable of your game class. This is not the same as making it a global variable. It's the opposite of using globals: each Game1 class instance will have its own local dataset. What you're doing here is fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by CrimsonSun
You are declaring fluidData fd as a member variable of your game class. This is not the same as making it a global variable. It's the opposite of using globals: each Game1 class instance will have its own local dataset. What you're doing here is fine.


! aha !

*moves into the silly corner*

Share this post


Link to post
Share on other sites
Quote:
In the heated threads on this forum there is a repeating notion that experienced programmers don't need to use global variables or singletons.
Utterly untrue, or at least misleading. Experienced OO-purist programmers generally choose not to use global variables or singletons, true. There's not many of them around. Most successful and pragmatic programmers choose not to adopt a pure OOP approach, and often write code using global variables. Nevertheless, even if you are not going to do pure OO programming it does make sense to know how to do pure OO programming, for much the same reason that it makes sense to know how to write algorithms using tail recursion even if you intend to use iteration your whole life.

The main point that I think is most often made is that if you use singletons "instead of" global variables, you are most likely using them wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by Crimsonus
I've been trying to become a better programmer, i've always heard that global variables are bad yet i have insisted on using them.
Global variables are not bad in themselves. However, correctly documented code has to explain how every function interacts with every global variable, which leads to either undocumented code (bad for long-term projects) or spending half an hour writing the documentation for even the tiniest function. Because of this, you need to either avoid using global state (such as in a pure object-oriented approach) or use known techniques for managing global state (such as procedural programming).

Quote:
In my current project i am making a fluid dynamics simulator, thus i have a few large datasets with 50k-100k objects each.
The size of data sets has nothing to do with how they are referenced.

Quote:
The project is made in C# XNA. In each frame(Update) there are a lot of functions that manipulate the datasets. "Draw" then calculates the colordata and outputs it to the screen.
Your functions assume that the datasets are fixed and always the same. You could have written them to apply to any dataset you choose (thereby making the functions easier to reuse on other datasets than the original ones) by providing a reference parameter to the target datasets instead of hard-wiring a global reference.

Quote:
I got the suggestion to make the datasets into a singleton class to avoid globals,
That suggestion is beyond stupid. It's akin to suggesting that a pipe leak should be solved by painting your walls white because modern houses have white walls—globals should be avoided because they are an example of global mutable state, but singletons are another example of global mutable state.

Quote:
problem is that i see myself making local copies of the datasets, thus making it a singleton seems counterproductive.
Making local copies has nothing to do with singletons. If anything, you cannot make copies of a singleton, so your sentence makes no sense. I suspect that you don't really know what a singleton is. Check this out.

Quote:
In the heated threads on this forum there is a repeating notion that experienced programmers don't need to use global variables or singletons.
It's true. You can write any program with almost no global variables (and those that you use would be a consequence of your framework choice). This is often desirable, for instance when code reuse and open/closed extensibility is importent. But it's not necessary: procedural programming can and will result in working software that does not suffer from most ill consequences of global variables, and it can be even faster to develop (given the right language support).

Quote:
So my question is this, how am i supposed to make the datasets persistent if i can't declare the datasets in a higher scope?
Persistence has nothing to do with scope. Scope represents who can access a name. However, a given piece of data may be represented by different names, and won't disappear (in C#) until all names are gone, so simply make sure that the scopes of the names overlap each other until you don't need the data anymore (this is easier than it seems).

Quote:
As an example, the sound manager has no need to know about the fluid datasets but unless the data is stored globally i'm going to lose all the data the instant the program enters the soundmanager.
No. You're not going to lose all the data unless you stop referencing it. So simply don't stop referencing it.

Share this post


Link to post
Share on other sites

This topic is 3314 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this