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 $$
Yes, in fact it's preferred since any programmer reading your code will understand that it's simply an iterator.
It depends. If you're iterating over some particular set of data then I think it makes more sense to use a descriptive name. (eg.
filecounter
as Dan suggested).However, if you're performing an arbitrary loop then
i
is acceptable. As one work mate described it to me -i
is a convention that means "this variable is only ever modified by thefor
loop construct. If that's not true, don't usei
"It helps if you name it something that describes what it is looping through. But I usually just use i.
I am going to go against the grain and say no.
For the crowd that says "i is understood as an iterator", that may be true, but to me that is the equivalent of comments like 'Assign the value 5 to variable Y. Variable names like comment should explain the why/what not the how.
To use an example from a previous answer:
Is it that much harder to just use a meaningful name like so?
Granted the (borrowed) variable name objectCollection is pretty badly named too.
i is fine, but something like this is not:
Very common for programmers to inadvertently swap the i and the j in the code, especially if they have bad eyesight or their Windows theme is "hotdog". This is always a "code smell" for me - it's kind of rare when this doesn't get screwed up.
"i" means "loop counter" to a programmer. There's nothing wrong with it.