代码大全学习-14-变量名的力量(Power of Variables Names)
2012-07-26 07:47
417 查看
这一章对变量名的讲述是相当的具体,作为一本手册真是名符其实。我不想在这里再重复那些具体详细的方法,可以去翻书,或者按照后面附上的checklist一条一条去检查。这里主要讲讲为什么要有那么多方法。
原因就是增强程序的可读性,从而降低程序的复杂性。所有的总总方法都围绕这一目的展开。像变量名要能准确完整的描述这个变量表示的内容,要描述它是什么(what),而不是怎么做(how),越具体越好,这样我们一看到变量名,就知道它是什么,增强可读性。再比如要制定一些命名规则,区分不同类型的变量,用什么样的前缀,用什么样的缩写,具体是什么样的具体分析,关键是要大家一起遵守,这样程序就会看起来像一个人写的,可读性大大增强。还有像要避免一些问题也都是为了方便阅读代码,理解代码。因为程序里全是各种各样的变量,变量的可读性好了,程序就容易看懂,复杂性就低,就不容易出错,维护性也好,质量就高。
相信这个原因有经验的同行都不存在什么疑问,直接附上checklist,要用的时候去查吧。
Checklist: Power of Variables Names
General Naming Considerations
Does the name fully and accurately describe what the variable represents?
Does the name refer to the real-world problem rather than to the programming-language solution?
Is the name long enough that you don't have to puzzle it out?
Are computed-value qualifiers, if any, at the end of the name?
Does the name use Count or Index instead of Num?
Naming Specific Kinds Of Data
Are loop index names meaningful (something other than i, j, or k if the loop is more than one or two lines long or is nested)?
Have all "temporary" variables been renamed to something more meaningful?
Are boolean variables named so that their meanings when they're True are clear?
Do enumerated-type names include a prefix or suffix that indicates the category-for example, Color_ for Color_Red, Color_Green, Color_Blue, and so on?
Are named constants named for the abstract entities they represent rather than the numbers they refer to?
Naming Conventions
Does the convention distinguish among local, class, and global data?
Does the convention distinguish among type names, named constants, enumerated types, and variables?
Does the convention identify input-only parameters to routines in languages that don't enforce them?
Is the convention as compatible as possible with standard conventions for the language?
Are names formatted for readability?
Short Names
Does the code use long names (unless it's necessary to use short ones)?
Does the code avoid abbreviations that save only one character?
Are all words abbreviated consistently?
Are the names pronounceable?
Are names that could be mispronounced avoided?
Are short names documented in translation tables?
Common Naming Problems: Have You Avoided...
...names that are misleading?
...names with similar meanings?
...names that are different by only one or two characters?
...names that sound similar?
...names that use numerals?
...names intentionally misspelled to make them shorter?
...names that are commonly misspelled in English?
...names that conflict with standard library-routine names or with predefined variable names?
...totally arbitrary names?
...hard-to-read characters?
原因就是增强程序的可读性,从而降低程序的复杂性。所有的总总方法都围绕这一目的展开。像变量名要能准确完整的描述这个变量表示的内容,要描述它是什么(what),而不是怎么做(how),越具体越好,这样我们一看到变量名,就知道它是什么,增强可读性。再比如要制定一些命名规则,区分不同类型的变量,用什么样的前缀,用什么样的缩写,具体是什么样的具体分析,关键是要大家一起遵守,这样程序就会看起来像一个人写的,可读性大大增强。还有像要避免一些问题也都是为了方便阅读代码,理解代码。因为程序里全是各种各样的变量,变量的可读性好了,程序就容易看懂,复杂性就低,就不容易出错,维护性也好,质量就高。
相信这个原因有经验的同行都不存在什么疑问,直接附上checklist,要用的时候去查吧。
Checklist: Power of Variables Names
General Naming Considerations
Does the name fully and accurately describe what the variable represents?
Does the name refer to the real-world problem rather than to the programming-language solution?
Is the name long enough that you don't have to puzzle it out?
Are computed-value qualifiers, if any, at the end of the name?
Does the name use Count or Index instead of Num?
Naming Specific Kinds Of Data
Are loop index names meaningful (something other than i, j, or k if the loop is more than one or two lines long or is nested)?
Have all "temporary" variables been renamed to something more meaningful?
Are boolean variables named so that their meanings when they're True are clear?
Do enumerated-type names include a prefix or suffix that indicates the category-for example, Color_ for Color_Red, Color_Green, Color_Blue, and so on?
Are named constants named for the abstract entities they represent rather than the numbers they refer to?
Naming Conventions
Does the convention distinguish among local, class, and global data?
Does the convention distinguish among type names, named constants, enumerated types, and variables?
Does the convention identify input-only parameters to routines in languages that don't enforce them?
Is the convention as compatible as possible with standard conventions for the language?
Are names formatted for readability?
Short Names
Does the code use long names (unless it's necessary to use short ones)?
Does the code avoid abbreviations that save only one character?
Are all words abbreviated consistently?
Are the names pronounceable?
Are names that could be mispronounced avoided?
Are short names documented in translation tables?
Common Naming Problems: Have You Avoided...
...names that are misleading?
...names with similar meanings?
...names that are different by only one or two characters?
...names that sound similar?
...names that use numerals?
...names intentionally misspelled to make them shorter?
...names that are commonly misspelled in English?
...names that conflict with standard library-routine names or with predefined variable names?
...totally arbitrary names?
...hard-to-read characters?
相关文章推荐
- [Code Complete] CH11 The Power of Variable Names 变量名的力量
- 读书笔记_代码大全_第11章_变量名的力量
- 【代码大全】第11章 变量名的力量
- 代码大全-第十章:变量的使用 学习心得
- 代码大全——变量名的力量
- 变量命名的力量 . 代码大全 . 启示
- 代码大全学习-21-表驱动方法(Table-Driven Methods)
- 用500行Julia代码开始深度学习之旅 Beginning deep learning with 500 lines of Julia
- 《代码大全》学习摘要(一)变量名的力量
- Jade模板引擎学习(二)语法:代码、变量、循环、过滤器及mixin
- Windows API 函数学习(14)---SN填写代码
- 随机的力量(1) - The power of random two choices
- 代码大全学习-7
- LeetCode编程练习 - Power of Three学习心得
- java学习日记_14:面向对象之成员变量与局部变量的区别。
- 机器学习之多变量线性回归(Linear Regression with multiple variables)
- 代码大全学习-23-软件质量概述(The software-Quality Landscape)
- 代码大全学习-10-高质量的函数(High-Quality Routines)
- python学习第二章变量和简单数据类型的部分课后练习自己尝试的代码
- 代码大全学习总结——直线型代码、条件语句