NSMutableArray vs Multiple NSStrings, NSIntegers, etc
Evening/morning campers. I'm moving ever closer to the release of my first app but just wanted some oppinions on data storage/use. I'm a bit OCD when it comes to code and like things to be as neat and tidy as possible.
In your oppinion is it better to store values accross several NSStrings, NSSintegers, foats etc or keep them all in one NSMutable array?
for example, instead of having the following 3 items:
NSString * ballhit;
NSInteger batpower;
NSString * cabbage;
I could just have one NSMutablearray and store all of the values in there. would this have any performance issues (with a lot more than 3 items of course)?? I know my code would look a lot neater but Do NSMutable arrays use more memory or cause more performance issues than using multiple NSStrings and NSIntegers?? My gut feeling is that one array would be more efficient but I would like your oppinions and experiences?
2) I fail to see how using the array would clean up your code. Instead of simply doing [self cabbage] you would have to change that to [[self variableList] objectAtIndex:2]. And if you're smart about it, you'll declare constants to keep track of this stuff, so add #define kCabbageIndex 2 in there somewhere, too. That's hardly cleaner.
And good luck to you when you return to this project in 6 months after having worked on other stuff.
Evening/morning campers. I'm moving ever closer to the release of my first app but just wanted some oppinions on data storage/use. I'm a bit OCD when it comes to code and like things to be as neat and tidy as possible.
In your oppinion is it better to store values accross several NSStrings, NSSintegers, foats etc or keep them all in one NSMutable array?
for example, instead of having the following 3 items:
NSString * ballhit;
NSInteger batpower;
NSString * cabbage;
I could just have one NSMutablearray and store all of the values in there. would this have any performance issues (with a lot more than 3 items of course)?? I know my code would look a lot neater but Do NSMutable arrays use more memory or cause more performance issues than using multiple NSStrings and NSIntegers?? My gut feeling is that one array would be more efficient but I would like your oppinions and experiences?
Thanks very much.
Arrays take a small amount of memory, and add a layer of indirection to fetching their contents. They will be marginally slower than accessing your strings directly.
I also think they will make the code more complex and harder to read than using strings directly.
How would code like this:
Code:
label.text = [stringArray objectAtIndex: 3];
be cleaner and easier to read than code like this:
Code:
label.text = playerName;
One time where I think arrays make sense is if you need to reference them by numeric index, like if you are setting a whole bunch of labels with a sequence of text strings, e.g:
Code:
for (i = 0; i < num_fields; i++)
{
UILabel* aLabel = [self.view viewWithTag: i];
aLabel.text = [labelStrings objectAtIndex: i];
}
Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.
Arrays are really for storing a collection of the same type of thing. Sure, since they're untyped in Obj-C, you can use them to store one of this, two of the other, etc., but that's not really what they're for. If you're starting to wonder how you should store a group of unrelated things, look into structures or classes. That's what they (structures, classes) are designed to do.
Arrays take a small amount of memory, and add a layer of indirection to fetching their contents. They will be marginally slower than accessing your strings directly.
I also think they will make the code more complex and harder to read than using strings directly.
How would code like this:
Code:
label.text = [stringArray objectAtIndex: 3];
be cleaner and easier to read than code like this:
Code:
label.text = playerName;
One time where I think arrays make sense is if you need to reference them by numeric index, like if you are setting a whole bunch of labels with a sequence of text strings, e.g:
Code:
for (i = 0; i < num_fields; i++)
{
UILabel* aLabel = [self.view viewWithTag: i];
aLabel.text = [labelStrings objectAtIndex: i];
}
Thank you both for the quick response.
I was thinking more along the lines of the .h file as opposed to the .m file.
Although there would be slightly more code in the .m file, there would only be one line in the .h file as opposed several lines? Am I taking my OCD a bit too far? Am I just making life harder for myself in the long run with no extra benefit? I think that's what Brian is trying to say with the 6 month thing - although I do keep extensive notes as to what's contained in my arrays?
Well, I'm saying a couple of things. You are fooling yourself if you think that masking all of your variables behind a single array is cleaning anything up. The information needs to be somewhere. Whether you are declaring constants for your indexes, or writing down the order of the variables on a sheet of paper, you have to know (and really should document) what order the items are in the array. You've transferred your "clutter" somewhere else, but haven't actually removed it.
Plus, you will have completely disguised your design intent. Your design needs 2 strings and an integer, that's why you have them. Your design does not need an array, otherwise you would already have one. So you are advertising - to other classes, to other people, to yourself - that this class contains only a single array, but that isn't accurate. In 6 months when you've forgotten how this project works, and want to work on it again, you'll have to go digging through whatever documentation you have in order to figure out what is important in this class. I'm not saying that separate variables will completely avoid the relearning curve, but they sure will shorten it.
Apply your OCD towards making your code accurate, and easy to read and understand.
Well, I'm saying a couple of things. You are fooling yourself if you think that masking all of your variables behind a single array is cleaning anything up. The information needs to be somewhere. Whether you are declaring constants for your indexes, or writing down the order of the variables on a sheet of paper, you have to know (and really should document) what order the items are in the array. You've transferred your "clutter" somewhere else, but haven't actually removed it.
Plus, you will have completely disguised your design intent. Your design needs 2 strings and an integer, that's why you have them. Your design does not need an array, otherwise you would already have one. So you are advertising - to other classes, to other people, to yourself - that this class contains only a single array, but that isn't accurate. In 6 months when you've forgotten how this project works, and want to work on it again, you'll have to go digging through whatever documentation you have in order to figure out what is important in this class. I'm not saying that separate variables will completely avoid the relearning curve, but they sure will shorten it.
Apply your OCD towards making your code accurate, and easy to read and understand.
A very fair and valid point, just what I was looking for. I will leave the code as it is and agree that it will only cause me more work. It's the old adage "If it aint broke, don't fix it". Thanks for your time Brian, I can tell that even the thought of me doing this has annoyed you so I know now for sure it's a bad idea as I can clearly see you're a man who knows his stuff. Thanks once again for your advice.