Ryoshi
Ryoshi

Reputation: 113

If concurrency isn't an issue, is there any reason to avoid using a global object?

I'm working on an iPhone/iOS game, and I was wondering if I could/should use a global object for statistic tracking between scenes. Now wait, before you jump down my throat - yes, it goes against everything I know about good code, but I don't think that concurrency is going to be an issue in this case. The structure of my game is similar to an Oregon Trail kind of affair with a sprinkling of Civilization thrown in for good measure - since it's not real-time I can ensure that only one process will be accessing the object in question at one time, and thus I'm pretty sure concurrency will not be an issue at all. Basically every scene will need access to most of the data (population, current technology level, current date, current map state) and so I'm wondering if I can get away with using a global object to store this data.

Everything I ever learned in college screams that this is a Bad Idea, but the sneaky programmer part of me keeps whispering "it won't be that bad, just this once couldn't hurt." And without worrying about concurrency, I'm kind of leaning towards the sneaky programmer. Is there any other reason I should avoid using a global object for this kind of statistic tracking?

Upvotes: 1

Views: 97

Answers (1)

Mick F
Mick F

Reputation: 7439

Can you explain what you mean by global object?

  1. If you're talking about the singleton design pattern, there's nothing wrong using it.
  2. If you're talking about an object instance that different threads can modify, there's nothing wrong doing this, if you're careful and use @synchronize on objects correctly
  3. But I think it iS wrong to assume your application is going to be mono thread during its whole lifecycle for the reason that iOS can do stuff behind the scene and that it would prevent you to do any asynchronous processing (and I also assume any game requires some non-trivial processing and this processing should be done on a background thread to let the main thread be as responsive as possible on user interactions)

Upvotes: 3

Related Questions