Reputation: 265
I've been programming for many years, however mostly outside industry. I'd like to make a concerted effort to clean up my programming style.
I've been rethinking some basic ideas and this is one - when is it appropriate to add a variable vs. combine/use existing variables as needed?
Obviously, if there's an intermediate value that needs to get re-used that would be a good reason to add a variable. However, when I'm adding variables for the sake of readability, the choice isn't as clear. For example, in C++, I may have a variable that looks like:
std::vector<std::map<char, float> > frequencies;
If I'm iterating over it, I may access things values like so:
x = frequencies[i]['c']
I can also introduce a temporary variable:
std::map<char, float> curr_freq = frequencies[i];
x = curr_freq['c'];
This is a fairly simple case, but you could imagine more complex nested structures. I find that I don't have a principled basis for choosing one or the other, when things start to feel too unreadable, I'll add variables, otherwise not, and since I don't have a basis for these decisions my code probably doesn't look very consistent. Is there a more principled basis for deciding when it's appropriate to add a variable?
Upvotes: 1
Views: 212
Reputation: 7015
In your specific case, the line:
std::map<char, float> curr_freq = frequencies[i];
Will actually create a new map
object by copying the map
stored at frequencies[i]
. Since you only want to index into your container, it seems wasteful to do all of this extra copying.
If you wanted to introduce a temporary variable (to make the code more readable etc), but didn't want to create additional overhead you could just grab a pointer/reference into your data structure:
std::map<char, float> &curr_freq = frequencies[i]; // grab reference, not a copy
float x = curr_freq['c'];
In general it's probably a good idea to make the code as readable as possible by using descriptive temporary variables etc. By making use of references/pointers etc you can typically do this without additional cost.
Hope this helps.
Upvotes: 1
Reputation: 5290
there isn't if it's an interpreter language and there's a trivial getter (which i would normally avoid), then i would use a variable to prevent extra closures on runtime.
in C you need to define the variables at the top of the function, so i would try to keep it to the minimum.
in java/php, it depends on how i'm iterating - foreach ($items as $i) or foreach($items as $k=>$v) or foreach(Object o : objectsArray) would all create the local variables for you, where for($i=0, $n=count($items); $i
otherwise, as long as the code is readable, i avoid using extra variables, but then again it could also depends on the language. i'd suggest- use OOP, short methods/functions and add comments so the code remain readable, and only use more variables to make it more readable when needed. Most important - make sure other people can understand your code, thats how you know you are all good
Upvotes: 0
Reputation: 20640
What ever increases the readability of the code is what you should do.
Others may need to maintain your code.
And, more importantly, YOU may need to maintain your code.
Upvotes: 0