Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a variable named i unacceptable? [closed]

As far as variable naming conventions go, should iterators be named i or something more semantic like count? If you don't use i, why not? If you feel that i is acceptable, are there cases of iteration where it shouldn't be used?

like image 841
VirtuosiMedia Avatar asked Sep 25 '08 00:09

VirtuosiMedia


People also ask

Which one is an acceptable variable name?

A valid variable name starts with a letter, followed by letters, digits, or underscores. MATLAB® is case sensitive, so A and a are not the same variable.

Which one is not a proper variable identifier?

Variable name may not start with a digit or underscore, and may not end with an underscore. Double underscores are not permitted in variable name. Variable names may not be longer than 32 characters and are required to be shorter for some question types: multiselect, GPS location and some other question types.

What is an example of a variable name?

The period, the underscore, and the characters $, #, and @ can be used within variable names. For example, A. _$@#1 is a valid variable name.


2 Answers

Depends on the context I suppose. If you where looping through a set of Objects in some collection then it should be fairly obvious from the context what you are doing.

for(int i = 0; i < 10; i++)
{
    // i is well known here to be the index
    objectCollection[i].SomeProperty = someValue;
}

However if it is not immediately clear from the context what it is you are doing, or if you are making modifications to the index you should use a variable name that is more indicative of the usage.

for(int currentRow = 0; currentRow < numRows; currentRow++)
{
    for(int currentCol = 0; currentCol < numCols; currentCol++)
    {
        someTable[currentRow][currentCol] = someValue;
    }
} 
like image 197
Josh Avatar answered Sep 23 '22 05:09

Josh


"i" means "loop counter" to a programmer. There's nothing wrong with it.

like image 45
Andy Lester Avatar answered Sep 25 '22 05:09

Andy Lester