Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating temporary variables to enhance readability

I would like to know if there are any significant disadvantages to creating a temporary variable for a value that is accessed multiple times within a small scope.

eg:

filep->waypt[rp->leg[j]].ID

or

(*(filep->route + filep->nroutes - 1))->number

These are examples from a recent project where I forced myself to avoid almost any simplification for variable references in order to improve my skills with C pointers (it was painful). However, the habit seemed to stick. I find that the more I try to introduce variables to simplify my code (readability and typing volume) the more confusing it becomes to remember exactly what each new variable references.

I've only ever had my code reviewed in an educational setting, and I would like to know what others find easier to digest and where are the performance tradeoffs (if any).

When would assigning a value who's memory address would require several arithmetic operations to calculate it's own variable start to cause performance issues? Would it make a difference if done once in a loop? What about once in a nested loop? What about a value assigned to it's own variable in a loop, but is then accessed in an inner loop?

How would this change in an interpreted language? Say in PHP (please excuse syntactical errors, I'm new to PHP):

$employees[$i][$phone]['Home']['number'];

vs

$home = $employees[$i][$phone]['Home']['number'];

And finally, if it's not too subjective (code readability shouldn't be!), which is considered the best practice for writing readable code? I write code that is as self-documenting as possible, but if variables begin to get too contrived and are referenced multiple times, I will assign them their own variable. However, the reason this works for me could be that I've gotten used to my own style.

like image 230
Emma Avatar asked Aug 05 '10 04:08

Emma


People also ask

What is the purpose of temporary variables?

In computer programming, a temporary variable is a variable with short lifetime, usually to hold data that will soon be discarded, or before it can be placed at a more permanent memory location.

What is the best way to declare a temporary variable and why?

Declare them just below the method selector. Assign them a value as soon as the expression is valid. Collecting Temporary Variable (2) saves intermediate results for later use. Caching Temporary Variable (3) improves performance by saving values.

Why is the use of temporary variables encouraged in Python?

It helps prevent statements getting too long and too complex.


2 Answers

There's a fairly good chance that any decent compiler will detect common sub-expressions and optimise them for you.

However, I would still opt for a temporary variable if it improved the readability of my code, despite the fact that it introduced extra storage (though minimal).

I consider maintainability of code far more important than a tiny storage hit. By way of (contrived) example, I would probably replace a whole lot of:

item[last_item-1].id = 14860;
item[last_item-1].name.first = "pax";
item[last_item-1].name.last = "diablo";

calls with:

tItem lastItem = &(item[last_item-1]);
lastItem->id = 14860;
: : :
// and so on.

As to interpreted languages, I have less information on that. I know quite a bit about how to optimise assembly code (though nowhere near as much as the demigods that write compilers, which is why I normally leave it to them). I know somewhat less about the virtual machines running inside most interpreters.

But I would still code for readability. I don't want to see a bucketload of $employees[$i][$phone]['Home']['number'] snippets stuck in my code. I would rather add

$homePhone = $employees[$i][$phone]['Home']['number'];

in there somewhere and just use $homePhone from that point onward.

like image 68
paxdiablo Avatar answered Oct 19 '22 14:10

paxdiablo


I would strongly encourage you to use variables for values that are reused at all. The key is to use variable names that actually describe what your are using the value for or what the value is. Also using a variable to store the value and then using the variable makes modification waaay easier in the future and much less error prone.

Here are some naming resources as well to help you with naming variables well. Also don't be afraid to comment your code so you know what each variable, function, class, etc.. was intended to do. Microsofts Naming guidelines

Stackoverflow Post on PHP naming guidelines

UPDATE: removed my example because it was wrong as pointed out in the comment below. I would still recommend using variables for values that are reused within the function as it improves readability and maintainability.

like image 27
Adrian Avatar answered Oct 19 '22 16:10

Adrian