On Feb 23, 7:17 pm, Jason Persampieri <[email protected]> wrote: > On Wed, Feb 23, 2011 at 2:02 PM, jemptymethod <[email protected]>wrote: > > > I inherited some code that essentially does this: > > > user = deserializeUser(params); //var seemingly omitted by design > > initViewPort(); > > > initViewPort() in turn calls: > > > initHeaderPanel(); > > initMenuPanel(); > > initQuotesPanel(); > > > Each of the above three methods subsequent to initViewPort() relies on > > the availability of the global "user" variable. I can think of two > > way to obviate this reliance on a non-name-spaced global variable, and > > one of those ways should be apparent based on my reference to "non- > > namespaced", and that would be to provide a namespace and a getter > > method, something such as: > > > namespace.context.getUser(); //returns object > > > So that the beginning of my code might be more like: > > > var user = deserializeUser(params); > > namespace.context.setUser(user); > > initViewPort(); > > > Then at the top of initHeaderPanel, initMenuPanel & initQuotesPanel I > > would do: > > > var user = namespace.context.getUser(); > > > So that is one possibility. The other way I'm considering is to pass > > user around thusly: > > > var user = deserializeUser(params); > > initViewPort(user); > > > And then: > > > initHeaderPanel(user); > > initMenuPanel(user); > > initQuotesPanel(user); > > > Am interested in the group's views on the pros and cons of these > > approaches. > > If 'user' is really only necessary for those three functions, then I'd > probably just pass it. However, if 'user' is used throughout the > application, then I'd go with the first option... maybe even take that a bit > further by having an 'ApplicationState' module that has a getter/setter for > 'user'.
I agree you've hit upon the determining factor. And indeed, I will need user from another module. So I will indeed need to expose user in some other way, such as an 'ApplicationState', (or 'context') module. However I've been wondering if I don't still want to pass user to those other three modules, while I have the reference. Indeed, I'm wondering if an applicable heuristic (an under-appreciated book, right up there with GOF Design Patterns in my opinion, is Riel's "Object- Oriented Heuristics") might be "If you have a reference, pass it". I presume it might save a bit of processing, rather than unnecessarily getting the reference again with 'namespace.context.getUser();'. Though I presume there is a bit of expense involved with merely passing the reference? Though in either case perhaps negligible? -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/[email protected]/ To search via a non-Google archive, visit here: http://www.mail-archive.com/[email protected]/ To unsubscribe from this group, send email to [email protected]
