循环变量的理想变量命名约定是什么? [英] What is an ideal variable naming convention for loop variables?

查看:129
本文介绍了循环变量的理想变量命名约定是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您正在编写一个简单小循环,那么您应该为计数器命名什么 ?

If you are writing a simple little loop, what should you name the counter?

提供示例循环!

推荐答案

我总是使用有意义的名称,除非它是单级循环,并且变量除了次数"外没有其他含义我经历过这个循环",在这种情况下,我使用i.

I always use a meaningful name unless it's a single-level loop and the variable has no meaning other than "the number of times I've been through this loop", in which case I use i.

使用有意义的名称时:

  • 同事更容易理解您的代码,
  • 在循环逻辑中查找错误更容易,并且
  • 文本搜索变量名以返回对相同数据进行操作的相关代码段更为可靠.

使用单个字母在此嵌套循环中查找错误可能很棘手:

It can be tricky to find the bug in this nested loop using single letters:

int values[MAX_ROWS][MAX_COLS];

int sum_of_all_values()
{
    int i, j, total;

    total = 0;
    for (i = 0; i < MAX_COLS; i++)
        for (j = 0; j < MAX_ROWS; j++)
             total += values[i][j];
    return total;
}

使用有意义的名称更容易:

whereas it is easier when using meaningful names:

int values[MAX_ROWS][MAX_COLS];

int sum_of_all_values()
{
    int row_num, col_num, total;

    total = 0;
    for (row_num = 0; row_num < MAX_COLS; row_num++)
        for (col_num = 0; col_num < MAX_ROWS; col_num++)
             total += values[row_num][col_num];
    return total;
}

为什么row_num? -被拒绝的替代方案

作为对其他答案和评论的回应,这些是使用row_numcol_num的一些替代建议,以及为什么我选择不使用它们:

Why row_num? - rejected alternatives

In response to some other answers and comments, these are some alternative suggestions to using row_num and col_num and why I choose not to use them:

  • r c :这比ij略好.如果我所在组织的标准是将单字母变量设置为整数,并且始终将其用作等价描述性名称的首字母,那么我只会考虑使用它们.如果我在函数中有两个以"r"开头的变量,系统就会崩溃,即使其他以"r"开头的对象出现在代码中的任何地方,可读性也会受到影响.
  • rr cc :在我看来,这很奇怪,但是我不习惯使用双字母循环变量样式.如果这是我组织中的标准,那么我想它会比rc稍微好一点.
  • row col :乍一看,它似乎比row_numcol_num更简洁,而且具有描述性.但是,我希望像行"和列"这样的裸名词来指代结构,对象或指向它们的指针.如果row可能表示行结构本身 行号,则会导致混淆.
  • iRow iCol :这传达了更多信息,因为i可能表示它是循环计数器,而RowCol告诉您它在计数什么.但是,我希望能够几乎以英语阅读代码:
    • row_num < MAX_COLS读为"行数 ber 小于 最大行(数量) col umn s ;
    • iRow < MAX_COLS最多表示为"整数循环计数器小于最大 > imum(数量) col umn s ".
    • 这可能是个人的事情,但我更喜欢一读.
    • r and c: This is slightly better than i and j. I would only consider using them if my organisation's standard were for single-letter variables to be integers, and also always to be the first letter of the equivalent descriptive name. The system would fall down if I had two variables in the function whose name began with "r", and readability would suffer even if other objects beginning with "r" appeared anywhere in the code.
    • rr and cc: This looks weird to me, but I'm not used to a double-letter loop variable style. If it were the standard in my organisation then I imagine it would be slightly better than r and c.
    • row and col: At first glance this seems more succinct than row_num and col_num, and just as descriptive. However, I would expect bare nouns like "row" and "column" to refer to structures, objects or pointers to these. If row could mean either the row structure itself, or a row number, then confusion will result.
    • iRow and iCol: This conveys extra information, since i can mean it's a loop counter while Row and Col tell you what it's counting. However, I prefer to be able to read the code almost in English:
      • row_num < MAX_COLS reads as "the row number is less than the maximum (number of) columns";
      • iRow < MAX_COLS at best reads as "the integer loop counter for the row is less than the maximum (number of) columns".
      • It may be a personal thing but I prefer the first reading.

      我会接受的row_num的替代方法是row_idx:索引"一词唯一地指数组位置,除非应用程序的领域在数据库引擎设计,金融市场或类似领域.

      An alternative to row_num I would accept is row_idx: the word "index" uniquely refers to an array position, unless the application's domain is in database engine design, financial markets or similar.

      我上面的示例尽可能地小,因此某些人可能看不到描述性地命名变量的意义,因为他们可以一次性掌握整个函数.但是,在实际代码中,功能会更大,逻辑也会更复杂,因此,体面的名称对于提高可读性和避免错误更为重要.

      My example above is as small as I could make it, and as such some people might not see the point in naming the variables descriptively since they can hold the whole function in their head in one go. In real code, however, the functions would be larger, and the logic more complex, so decent names become more important to aid readability and to avoid bugs.

      总而言之,我对所有变量命名(不仅仅是循环)的目标是完全明确.如果任何人读取我的代码的任何部分并且无法立即计算出变量的含义,那么我就失败了.

      In summary, my aim with all variable naming (not just loops) is to be completely unambiguous. If anybody reads any portion of my code and can't work out what a variable is for immediately, then I have failed.

      这篇关于循环变量的理想变量命名约定是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆