Skip to content

Commit 17f1c0e

Browse files
committed
Update ThePowerofVariableNames.md
1 parent 910044e commit 17f1c0e

1 file changed

Lines changed: 29 additions & 0 deletions

File tree

CodeComplete/ThePowerofVariableNames.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,33 @@
5252

5353
begin/end fist/last locked/unlocked min/max old/new
5454
opend/closed visible/invisible source/target source/destination up/down
55+
56+
## Naming specific types of data 为特定类型的数据命名
57+
58+
### Naming loop indexes 为循环变量命名
59+
60+
循环是一种常见的计算机编程编程特征。i,j,k 约定俗成。 如果在循环外部使用,那么就使用要比i,j,k 更有意义的名字。
61+
比如读取文件记录的记录数量recordCount
62+
```
63+
java 描述性较好的循环变量名
64+
recordCount=0;
65+
while(moreScores()){
66+
score[recordCount] = GetNextScore();
67+
recordCount++;
68+
}
69+
// line useing recordCount
70+
```
71+
因为代码容易扩充修改并容易忘记i这类变量的具体意义,故有经验的程序员不使用i这样的名字。
72+
73+
导致循环变长的一个原因是出现循的环嵌套使用。
74+
```
75+
java 嵌套循环中的好的循环变量
76+
for( teamIndex = 0;teamIndex < teamCount;teamIndex++){
77+
for(eventIndex = 0;eventIndex < eventCount[teamIndex];eventIndex++){
78+
score[teamIndex][eventIndex]=0;
79+
}
80+
}
81+
```
82+
谨慎的使用循环下标变量名可以避免产生下标串话(index cross-talk)的常见问题:i,j用混。
83+
score[teamIndex][eventIndex]比 score[i][j] 确实让人更容易懂。
5584

0 commit comments

Comments
 (0)