是一个名为i的变量不可接受吗? [英] Is a variable named i unacceptable?

查看:120
本文介绍了是一个名为i的变量不可接受吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

至于变量命名约定,应该将迭代器命名为 i 或更像语义的 count ?如果你不使用 i ,为什么不呢?如果你觉得 i 是可以接受的,是否有不应该使用的迭代情况?

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?

推荐答案

取决于我想的上下文。如果你在一些
集合中循环遍历一组对象,那么从上下文中你应该是很明显的。

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.

for(int i = 0; i < 10; i++)
{
    // i is well known here to be the index
    objectCollection[i].SomeProperty = someValue;
}

但是,如果不能从上下文中立即清除,或者如果要对索引进行修改,则应使用更能指示用法的变量名。

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.

for(int currentRow = 0; currentRow < numRows; currentRow++)
{
    for(int currentCol = 0; currentCol < numCols; currentCol++)
    {
        someTable[currentRow][currentCol] = someValue;
    }
} 

这篇关于是一个名为i的变量不可接受吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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