So, this is yet another 'good' programming practice question. I did search around a bit, but something like this is often hard to define in just a few words.
To the question: from a professional perspective, is it better programming practice to keep code streamlined and short (not necessarily more efficient) or to explicitly define instance variables only to assign them and immediately return? For example:
FILE * foo(){
FILE * retVal;
foo2();
retVal = foobar();
return retVal;
}
From the above, we can see immediately that foobar
returns a FILE *
. So, from this style of programming we can more quickly extract important information. This is true compared to something like this:
FILE * foo(){
foo2();
return foobar();
}
Which, of course, accomplishes the same thing. However, one must look more deeply to find the same information. I tend to favor the latter style of programming simply for the reason that it looks better. Due to the nature of how this program will run, I doubt there are any immediate performance gains from using either one since memory is still necessary for either choice - the difference is whether or not the user or the compiler allocates it.
As another example of keeping code short and concise:
int foo(){
int i = 0;
while(foobar())
i++:
return i;
}
TL:DR Question >> Is it better to explicitly show what is being done, or is it okay, in favour of brevity and conciseness, shorten code that accomplishes the same task but does not necessarily provide performance gain?
Short version
Only introduce new variable when they add non obvious informations. Usually that's the case when the variable is used to replace a somewhat complex expression
Long version
As in everything, it depends. I think the best way of approaching this is by doing a cost benefit analysis of the situation.
The cost of using an intermediate variable (purely from code quality/understanding point of view, i am pretty sure any decently modern compiler will optimize those away) is i would say the effort of parsing the variable, understand the definition in context, and more importantly relate the latter use of that variable to the working model of program that whomever is reading your code has in his mind.
The benefit is that a new element, by introducing more information, can help the reader to either form a simpler mental model of the code base, or a more precise model. And the for a new variable declaration, most information are contained in either the type or the name.
Consider for example the two following example:
In the first example your reader would need to understand what std::min does, what are 'd' and 'map', what are their type, what the type of the element of map etc... In the second example by providing meaningful variable name, you essentially save the reader from having to understand the computation , thus allowing him to have a simpler mental model of the code while roughly keeping the same amount of important information.
Now compare that to :
In this case, i think having personAge doesnt add any meanifull information (the variable and method name convey the same information), and thus is not really helping the reader in any way.