views:

48

answers:

3

In my iPhone development, I've always used global variables for lots of stuff. The style guide in my new job says we should use context parameters instead. So I need to figure out what that means and how to do that.

Can anyone explain in more detail what this means -- or point me to some code that works this way?

Thanks

A: 

It sounds like there may be a clash in nomenclature. From this definition of Context Parameters, they seem to be concerned storing global state for the duration of a session. Perhaps, you could use a 'contextParameters' NSDictionary within NSUserDefaults to store your globals. To the extent that your globals might need to be exported in their entirety (for debugging, for state saving) this might be useful in the long run.

westsider
The general idea is that if you have a variable, there should be a way to create more than one of it. So for example, if your view controller calls on a model object to get information which it uses to populate the view, there should be a way to have more than one of that model object. That much I get. What I don't get is how to structure my overall program so things work that way. I'm used to having a singleton model object where everything lives. That's not going to work anymore.
William Jockusch
Not sure I understand but is your model object's structure known at compile time? Could it be represented as an NSManagedObject and stored in a SQLite store, for example? If so, then you could use a data model and populate your store with instances of this data model. In other words, would Core Data be something worth exploring?
westsider
A: 

The style guide might just be generically saying to keep your variables scoped based on the context of their usage. For example if you have a variable that you need for the lifetime of a class instance then make it a member variable of that class. If it is something that you need for the lifetime of the app then put it in an application wide object (but not a global variable).

fhj
A: 

If you use a global object (which could mostly be a big C struct containing all your former global variables) instead of individual naked global variables, you might be able to copy the object, serialize it to save it or create a unified core dump, eventually add setters/listeners, etc.

If you break the global object up, based on the shared scope or the required context of groupings of instance/struct variables, then the fractional objects might end up being good candidates for the M portion of an MVC repartitioning of your code for better reuse, extensibility, etc.

hotpaw2