views:

57

answers:

2

Does creating a lot of private variables that may never be used increase file size and or memory usage of your application?

+1  A: 

Probably not (assuming that by not using them you aren't giving them any values, or if you are giving them values that those values aren't huge objects), but why confuse yourself and anyone that follows up on your code? You shouldn't have a variable unless you're going to use it.

invertedSpear
I know I shouldn't create the variable if I'm not going to use it, but I need a variable that can be used in other functions, and I don't know how to create public variables outside of a function using xml data. Once I load my xml data, I am then populating the variables that correspond to the xml length. For example, I create 500 public variables without values. Then I load my xml data. I then have an if statement that says "if the xml.length is >= 38 then variable this38=xml[38] and so on until i'm through all of the xml matching nodes.
Phil
Flex has great XML handling functions (look up e4x). You can do anything you need usually directly from the XML that you bring in. This could eliminate your need. Alternatively look at using an array, you can just push those XML items into the array and it will grow as needed.
invertedSpear
others have suggested arrays, but I can understand variables being declared directly and then being assigned a value...arrays allude me because they are not as concrete to me as straight variable declarations. Do you know of any array examples that would allow me to understand the concept of arrays better?
Phil
for what you're doing you want Associative arrays in an arrayCollection. I don't know of a good resource for you because it really depends on where your understanding is. An array goes ["a","b","c"] an associative array goes [letter1:"a" letter2:"b" letter2:"C"] see how each one gets a name. an arraycollection is like having an array of associative arrays. Honestly though I would look into e4x, I think that's going to be your best option, why convert to another variable when you already have it all in your XML
invertedSpear
A: 

For most languages it will be a no (most compilers are smart enough to detect unused variables and optimize accordingly), but it obfuscates the code somewhat. Best practices is to not have a lot of unused variables, makes the code easier to read and maintain.

Terry
If I make 500 - 1000 unused but available variables, I will only have to edit my xml file I will never have to edit my code again. But I agree, I don't like messy code.
Phil