名副其实 use intention-revealing names

  变量、函数或类的名称应该已经答复了所有的大问题。它该告诉你,他为什么会存在,他做什么事,应该怎么用。我们应该选择都是致命了计量对象单位的名称。

避免误导 avoid disinformation

  必修避免留下演示代码本意的错误线索。应当避免使用与本意相悖的词。例如:hp、aix和sco都不该用做变量名,因为它们都是UNIX平台或类UNIX平台的专有名称。不要用accountList来命名一组账号,除非它真的是List类型。可以用accountGroup、bunchOfAccounts代替。还要提防使用不同之处较小的名称。以同样的方式拼写出同样的概念才是信息。拼写前后不一致就是误导。误导性名称是必须不能存在的。如避免用小写的l和大写的O作为变量名,他们看起来很像常量1和0.

做有意义的区分 make meaningful distinctions

  同一作用范围内两样不同的东西不能重名,你可能有时随手改掉其中一个名称,有时干脆已错误的拼写充数,这样很容易导致更正拼写错误后导致编译器出错。光是添加数字系列或是废话远远不够,若名称必须相异,其意思也应不同。以数字系列命名是依意义命名的对立面。这样的命名纯属误导,完全没有提供正确信息,没有提供导向作者意图的线索。只要体现出有意义的区分,使用a和the这样的前缀就没错。如可以把a用在域内变量,而把the用于函数参数。Variable一词永远不应当出现在变量中。Table一词用于不应当出现在表名中。

  Number-series naming is the opposite of intentional naming.

  Noise words are another meaningless distinction.Noise words are redundant.

  Note that there is nothing wrong with using prefix conventionslike a and the so long as they make a meaningful distinction.

  Noise words are redundant.The word variable should never appear in a variable name.Name is better than nameString.

使用读得出来的名称 use prononceable names

  不要盲目的使用缩写,要使用能够知道明确意思的词来命名。

使用可搜索的名称 using searchable names  

  单字母名称和数字常量有个问题,就是很难在一大篇文字中找出来。e也不是便于搜索的好变量名,他是英文中最常用的字母,在每个程序,每段代码中都有可能出现。由此可见,长名称胜于短名称,搜得到的名称胜于用自造编码代码写的名称。单字母名称仅用于短方法中的本地变量。名称长短与其作用域大小相对应。

  My personal preference is that single-letter names can only be used as local variables inside short method.The length of a name should correspond to the size of its scope.If a varible or constant might be seen or used in multiple places in a body of code, it is imperative to give a search-friendly name.

避免使用编码 avoid encoding

  把类型或作用域编进名称里面,徒然增加了解码的负担。

避免思维映射 avoid mental mapping

  不应当让读者在脑中把你的名称翻译为他们所熟知的名称。这种问题经常出现在选择使用问题领域术语还是解决方案领域术语时。

  One difference between a smart programmer and ad professional programer is that the professional understands that clarity is king.Professional use their powers for good and write code that others can understand.

类名 class names

  类名和对象名应该是名词或名词短语。A class name should not be a verb.

方法名 method names

  方法名应当时动词或动词短语。属性访问器、修改器和断言应该根据其值命名,并依Javabean保准加上get、set和is前缀。重载构造器时,使用了描述了参数的静态工厂方法名通常好于使用默认的构造器。

  Complex fulcruPoin = Complex.FromRealNumber(23.0);

better than:

  Complex fulcrumPoint = new Complex(23.0);

别扮可爱 don't be cute

  不要使用俗话或俚语来命名。

每个概念对应一个词 pick one word per concept

  给每个抽象概念选一个词,并且一以贯之。使用fetch、retrieve和get来给多个类中的同种方法命名,很容易忘记哪个类中的哪个方法使用的是fetch、retrieve或get。

别用双关语 don't pun

  避免同一单词用于不同的目的。比如使用add来命名函数,表示通过该方法把两个现存值增加起来来获取新值。当一个类中有了要添加一个新的方法,如把单个参数放到群集中,此时不应该把该方法叫做add,应该用add或append之类的词来命名才对。

  add meaningful context

使用解决方案领域名称 use solution domain names

  只有程序员才会读你的代码。所以尽量用那些计算机科学术语、算法名、模式名、数学术语。

使用源自所涉问题领域的名称

  如果不能使用程序员熟悉的术语来给手头的工作命名,那么就采用从所涉及问题领域而来的名称。

  Choosing technical names for those things is usually the most appropriate course.So go ahead and use computer science terms, algorithm names, pattern names, math terms and so froth.

添加有意义的语境 use problem domain names

  很少有名称是能自我说明的。你需要有良好命名的类、函数或名称空间来放置名称,给作者提供语境。若没有这样做,给名称添加前缀就是最后一招了。

  When there is no  'programmer-eese' for what you'er doing, use the name from the problem domain.

  Seperating solution and proble domain concepts is part of the job of a good programmer and designer.

Add meaninful context

不要添加没用的语境 don't add gratuitous context

  对于GasStationDeluxe类来说,没有必要在每个属性前面加上GSD。只要短名称足够清楚,就要比长名称好。

  Shorter names are generally better than longer ones, so long as they are clear. Add no more context to a name than is necessary.