Wednesday, January 5, 2011

By any other name

Names are important. Specifically, names of variables.

Names of variables convey meaning to the people who read the code.

The variable name 'i' ('I' in FORTRAN) is special. It carries no meaning. (Similar variable names are 'j', 'k', 'i2', 'ii', and other single-letter names.) For some loops, a meaningless name is appropriate, because the loop mechanism is less important than the body of the loop.

Consider the following code:

for (int i = 0; i < sizeof(values); i++)
{
sum += values[i];
}

In this code, the important bits are the array 'value' and the scalar 'sum'. The objective is to calculate the sum of the values in the array. The looping mechanism is not important.

The following code is equivalent:

for (int index = 0; index < sizeof(values); index++)
{
sum += values[index];
}

I find this code a little harder to read, since the name 'index' carries some context. Yet the name 'index' is also fairly weak. Let's look at another example:

for (int loop_index = 0; loop_index < sizeof(values); loop_index++)
{
sum += values[loop_index];
}

In this sample, the name 'loop_index' draws our attention. We think about the loop, and therefore spend less effort on the action of the loop.

Sometimes variables change their meaning.

int i = 0;
while (i < sizeof(values) && values[i]->some_function())
{
i++;
}
if (i < sizeof(values))
{
values[i]->other_function();
}

In the 'while' loop, the variable 'i' is a dynamic index into the array, moving from one item to another. In the later 'if' statement, the variable is an index, but now it points to an array member that holds some special property that deserves our interest. Perhaps not so important. But consider the following code:

int i = 0;
while (i < sizeof(values) && values[i]->some_function())
{
i++;
}
// lots of code
if (i < sizeof(values))
{
values[i]->other_function();
}

The 'lots of code' block separates the initialization of 'i' and the use of 'i'. If you're not paying attention, you may miss the use of 'i'. Or you may forget the significance of 'i'. This case calls for a more descriptive name, one that conveys the reason for interest.

In the early days of programming, individual variables were tied to memory, so more variables meant more memory, and the limitations of the day pushed us to minimize variables. Today, we can afford multiple variables. I say, use multiple variables, assigning the value from one variable to another, and gain meaning from appropriate names.

No comments: