1、变量名要完全、准确的描述出改变量所代表的事物。
2、容易阅读,没有歧义。
3、字数10~15个最佳,如果<10,检查是否够清晰表达。
好的变量名如:teammembercount、teampointmax
4、表示计算结果的变量放在最后:total、sum、average、max、min、 record、string、point……
如:revenuetotal、expensetotal
5、合理使用对仗词
create | destroy |
---|---|
first | last |
begin | destroy |
creat | destroy |
get | put |
get | set |
increment | decrement |
insert | delete |
lock | unlock |
min | max |
next | previous |
old | new |
open | close |
show | hide |
source | target |
start | stop |
up | down |
6、为循环下标命名
例如:for(teamindex=0;teamindex<teamcount;teamindex++)
7、好的代码不需要猜测应该能直接读懂。
8、不使用与变量含义完全无关的名字。
9、避免在名字中包含易混淆的字符(避免相似)