0

Hi so as I mentioned in the title I have a bunch of loops within loops in my code and most of them use a variable to loop through stuff. I call my variables ia, then ib then ic and so on. What is good form for the naming of these variables?

Here is some code that might help make sense of what I am saying

for (var ic = 0; ic <= currState.length; ic++) { //loop the columns and check if there is a 
  if (currState[ic] == 0) {
    for (var id = 1; id <= currState.length; id++) { //loop the rows of a column

      tryPos = [id, ic + 1]; //id -> row | ic -> column

      if (checkClash(currState, tryPos) == false) {

        currState[ic] = id
        break;
      }
    }
  }
}
Nimajik
  • 95
  • 1
  • 1
  • 10
  • 4
    `row` and `col` seem pretty decent names to me – axelduch Dec 03 '15 at 21:20
  • 1
    Possible duplicate of [What is an ideal variable naming convention for loop variables?](http://stackoverflow.com/questions/101070/what-is-an-ideal-variable-naming-convention-for-loop-variables) – Frank Bryce Dec 03 '15 at 21:22
  • I tend to name them after Simpson characters, starting from Maggie. It doesn't matter. he name of a counter variable rarely influences clarity. – Markus W Mahlberg Dec 03 '15 at 21:27

2 Answers2

1

The name of your variables requires good descriptive skills and a shared cultural background.

In this case you should use row and col but don't forget the scope of your variables.

I would like recommend you read the Book: Clean Code.

Especially you can review some of the proposed rules on this post (http://www.itiseezee.com/?p=83).

caballerog
  • 2,679
  • 1
  • 19
  • 32
0

looping over a table or grid, row and column are perfectly fine variable names.

for more generic nested loops i, j, and k (in that order) are typical counter variables even though they are less aptly named

PhilVarg
  • 4,762
  • 2
  • 19
  • 37