可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
We know that, somehow, we use i
and j
variables in loops very commonly. If one need a double for
loop, it's very likely to use something like the following:
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
// do some stuff...
}
}
However, if I need a third for
loop in these loops, I don't have any naming convention for the third iterator. I, likely use the following variables: r
, k
, ii
, jj
etc...
Is there a naming convention for the third (and so on...) loop's iterator?
回答1:
The most important thing for readability should be obvious names.
i and j aren't the most obvious, but may be ok for simple cases. Consider this (admittedly somewhat ill thought out) example;
static void Main(string[] args)
{
for(int i = 0; k < 100; k++)
for (int j = 0; k < 100; k++)
for (int k = 0; k < 100; k++)
Console.WriteLine("" + i + "-" + j + "-" + k);
}
vs
static void Main(string[] args)
{
for(int survey = 0; survey < 100; survey++)
for (int question = 0; question < 100; question++)
for (int option = 0; option < 100; option++)
Console.WriteLine("" + survey + "-" + question + "-" + option);
}
It's quite easy to see which makes more sense to read. But while we're at it, how about making it even more readable while eliminating your naming problem even more;
static void Main(string[] args)
{
for(int survey = 0; survey < 100; survey++)
PrintSurvey(survey);
}
private static void PrintSurvey(int survey)
{
for (int question = 0; question < 100; question++)
PrintQuestion(survey, question);
}
private static void PrintQuestion(int survey, int question)
{
for (int option = 0; option < 100; option++)
PrintOption(survey, question, option);
}
private static void PrintOption(int survey, int question, int option)
{
Console.WriteLine("" + survey + "-" + question + "-" + option);
}
Maybe overkill/verbose for this simple loop, just wanted to make the point that there are more ways you can deal with the naming problem for nested loops than just finding unique names.
回答2:
There is no established convention for these things. i
is fine for basic loops. For more complex loop or where the implementation isn't obvious at a glance, you should consider naming the iterators explicitly, e.g. surveyIndex
and questionIndex
, rather than i
and j
.
回答3:
It's not uncommon to see nested loops in computer vision applications and, as other said, there is no convention, at least on the order of the letters, but generally people tend to use the letters i,j,k,l,m,n,q,h...
This is for example a possible implementation of a 2-dimensional discrete convolution used for filtering images:
/* The Input image I and Output image O have M*N pixels.
* The kernel H has size (2k+1)*(2k+1)
*/
for (i=k; i<M-k; i++) {
for (j=k; j<N-k; j++) {
temp=0;
for (m=-k; m<=k; m++)
for (n=-k; n<=k; m++)
temp = temp + I[i-m,j-n] * H[m+k,n+k];
O[i,j] = temp;
}
}
Here you can see another implementation in C++.
回答4:
I actually almost never use i, I much prefer something like cnt
, or index
, or id
. For one cnt
is still short and quick to type. Also people will immediately know what it is when they look at your code, it's a counter and most importantly when you try to use Search for something like "i"
you will get a lot of junk you don't want to see
include <iostream>
int main ()
//hi there, this is a comment
for (int i = 0; i < 5; i++)
While if you Ctrl+F
for index instead of i, or even more so for cnt you will only see what you expect to, so it's much more comfortable to navigate.
For more complex loops though you should definitely type the full name of the variable though, you're just going to confuse yourself otherwise