ios - Is using too many static variables in Objective-C a bad practice? -
will usage of static variables expose them danger of being modifiable anywhere ?(in context of objective-c). if yes, can suggest best alternatives using shared variables across classes ?
is using many static variables in objective-c bad practice?
yes. of course, "too many" has not been quantified , subjective. really, global/static variables thing -- convenient introduce , difficult debug , eliminate. rare case design. i've found life far easier without them.
will usage of static variables expose them danger of being modifiable anywhere? (in context of objective-c).
it depends on declared , how used. if pass reference part of program, modifiable 'anywhere'.
examples:
if place them 1 file can "see" variable (e.g. in .m file following includes), succeeding implementation may use (unless pass reference outside world).
if declare variable inside function, shared among each translation , copied each translation in c/objc (but rules different in c++/objc++).
if yes, can suggest best alternatives using shared variables across classes?
just avoid using globals altogether. create 1 or more type/object hold data, pass instance of implementations.
singletons middle ground, in have type of global variable/object based abstraction. singletons still hassle -- categorized global variables , banned in codebase.
Comments
Post a Comment