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?
相关问题
- Batch - Set variables in for loop
- JavaScript, confusing syntax when declaring a vari
- Efficiently chunk large vector into a vector of ve
- How can I create a new variable based on condition
- Why is a variable not updating after changing its
相关文章
- How can I unpack (destructure) elements from a vec
- Python relative import with more than two dots
- Insert into vector with conditional iterator
- Loss of variables switching orientations
- Bind a char to an enum type
- What is the limit in size of a Php variable when s
- Should I keep bad naming conventions?
- Makefile and use of $$
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.
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.
i is so common that it is acceptable, even for people that love descriptive variable names.
What is absolutely unacceptable (and a sin in my book) is using i,j, or k in any other context than as an integer index in a loop.... e.g.
The use of i, j, k for INTEGER loop counters goes back to the early days of FORTRAN.
Personally I don't have a problem with them so long as they are INTEGER counts.
But then I grew up on FORTRAN!
i is definitely acceptable. Not sure what kind of justification I need to make -- but I do use it all of the time, and other very respected programmers do as well.
Social validation, I guess :)
Here's another example of something that's perfectly okay:
As long as you're using it temporarily inside a simple loop and it's obvious what you're doing, sure. That said, is there no other short word you can use instead?
i
is widely known as a loop iterator, so you're actually more likely to confuse maintenance programmers if you use it outside of a loop, but if you use something more descriptive (likefilecounter
), it makes code nicer.