• Use Intention-Revealing Names

    The name should tell you why it exists, what it does, and how it is used.

    e.g.

    Bad code:

    public List<int[]> getThem(){
    List<int[]> list1 = new ArrayList<int[]>();
    for (int[] x : theList)
    if (x[0] == 4)
    list1.add(x);
    return list1;
    }

    Good code:

    public List<int[]> getFlaggedCells(){
    List<int[]> flaggedCells = new ArrayList<int[]>();
    for (int[] cell : gameBoard)
    if (cell[STATUS_VALUE] == FLAGGED)
    flaggedCells.add(cell);
    return flaggedCells;
    }

    Better code:

    public List<Cell> getFlaggedCells(){
    List<Cell> flaggedCells = new ArrayList<Cell>();
    for (Cell cell : gameBoard)
    if (cell.isFlagged())
    flaggedCells.add(cell);
    return flaggedCells;
    }

  • Avoid Disinformation
  • Make Meaningful Distinctions

    Bad code:

    public static void copyChars(char a1[], char a2[]) {
    for (int i = 0; i< a1.length; i++) {
    a2[i] = a1[i];
    }
    }

    Good code:

    Change the name a1, a2 to source and destination.

  • Use Pronounceable Names

    这条对于我们中国人来说就不合适了,命名时还是不要用汉语拼音。

  • Use Searchable Names

    The length of a name should correspond to the size of its scope.

  • Avoid Encodings
    • Hungarian Notation

      特定时代的产物,随着一些现代的集成开发环境的引入,已不建议使用了。(参考维基百科词条:匈牙利命名法

    • Member Prefix
    • Interfaces and Implementations

      leave interface unadorned.

      prefer to encoding implementations than interfaces.

      e.g. prefer to use the name ShapeFactoryImp rather than IShapeFactory.

      关于这一点,在上面提到的“匈牙利命名法”维基百科词条最后也有相关说明:

      .NET Framework,微软新的软件开发平台,除了接口类型一般不适用匈牙利命名法。在 .NET 中,习惯在接口类型前放一个 I (例如 Windows Forms 中的 IButtonControl 接口。).NET Framework 指导方针建议程序员不要用匈牙利命名法,但是没有指明不要用系统匈牙利命名法还是匈牙利应用命名法,或者是两者都不要用。

      与此对比,Java 的标准库中连接口类型也不加前缀。

      即 Java 的命名法则与作者的观点一致。

  • Avoid Mental Mapping

    Clarity is king.

  • Class Names

    noun or noun phrase names

  • Method Names

    verb or verb phrase names

  • Don’t Be Cute

    Say what you mean. Mean what you say.

  • Pick One Word per Concept
  • Don’t Pun
  • Use Solution Domain Names

    use computer science terms, algorithm names, pattern names, math terms and so forth.

  • Use Problem Domain Names

    When there is no “programmer-eese” for what you’re doing.

    Separating solutions and problem domain concepts is part of the job of a good programmer and designer. The code has more to do with problem domain concepts should have names drawn from the problem domain.

  • Add Meaningful Context

    Bad code:

    private void printGuessStatistics(char candidate, int count) {
    String number;
    String verb;
    String pluralModifier;
    if (count == 0) {
    number = "no";
    verb = "are";
    pluralModifier = "s";
    } else if (count == 1) {
    number = "1";
    verb = "is";
    pluralModifier = "";
    } else {
    number = Integer.toString(count);
    verb = "are";
    pluralModifier = "s";
    }
    String guessMessage = String.format(
    "There %s %s %s%s.", verb, number, candidate, pluralModifier);
    print(guessMessage);
    }

    Good code:

    public class GuessStatisticsMessage {
    private String number;
    private String verb;
    private String pluralModifier; public String make(char candidate, int count) {
    createPluralDependentMessageParts(count);
    return String.format(
    "There %s %s %s%s.",
    verb, number, candidate, pluralModifier);
    } private void createPluralDependentMessageParts(int count) {
    if (count == 0) {
    thereAreNoLetters();
    } else if (count == 1) {
    thereIsOneLetter();
    } else {
    thereAreManyLetters(count);
    }
    } private void thereAreNoLetters() {
    number = "no";
    verb = "are";
    pluralModifier = "s";
    } private void thereIsOneLetter() {
    number = "1";
    verb = "is";
    pluralModifier = "";
    } private void thereAreManyLetters(int count) {
    number = Integer.toString(count);
    verb = "are";
    pluralModifier = "s";
    }
    }

  • Don’t Add Gratuitous Context

Clean Code – Chapter 2: Meaningful Names的更多相关文章

  1. Clean Code – Chapter 3: Functions

    Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...

  2. Clean Code–Chapter 7 Error Handling

    Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...

  3. Clean Code – Chapter 4: Comments

    “Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...

  4. Clean Code – Chapter 6 Objects and Data Structures

    Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...

  5. Clean Code – Chapter 5 Formatting

    The Purpose of Formatting Code formatting is about communication, and communication is the professio ...

  6. “Clean Code” 读书笔记序

    最近开始研读 Robert C.Martin 的 “Clean Code”,为了巩固学习,会把每一章的笔记整理到博客中.而这篇博文作为一个索引和总结,会陆续加入各章的笔记链接,以及全部读完后的心得体会 ...

  7. clean code meaningful names

    ---恢复内容开始--- Meaningful Names: use Intention-Revealing Names //nice,Everyone who reads your code (in ...

  8. [转]Clean Code Principles: Be a Better Programmer

    原文:https://www.webcodegeeks.com/web-development/clean-code-principles-better-programmer/ ----------- ...

  9. 代码整洁之道Clean Code笔记

    @ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Function ...

随机推荐

  1. hibernate.cfg.xml配置(Oracle+c3p0)

    说明:数据库:Oracle10g:连接池:c3p0 结构: 一.配置hibernate.cfg.xml <?xml version="1.0" encoding=" ...

  2. MVC4多语言IHttpModule实现

    最近项目需要多语言环境了. 由于项目页面较多,逐个Action去读取资源文件不大现实.就想到了使用 IHttpModule配合MVC的路由规则来实现. 首先创建以个mvc4的应用程序,添加资源文件夹( ...

  3. sort-based shuffle的核心:org.apache.spark.util.collection.ExternalSorter

    依据Spark 1.4版 在哪里会用到它 ExternalSorter是Spark的sort形式的shuffle实现的关键.SortShuffleWriter使用它,把RDD分区中的数据写入文件. o ...

  4. 李洪强iOS开发之【零基础学习iOS开发】【02-C语言】06-变量与内存

    在前面一节中简单介绍了变量的使用,当我们定义一个变量的时候,系统就会为变量分配一块存储空间.而变量的数值在内存中是以二进制的形式存储的,这讲来深入研究变量在内存中的一些存储细节. 一.字节和地址 为了 ...

  5. cocos2dx addchild坐标问题

    a.addchild(b); 会把a->getBoundingBox矩形的左下角坐标点和b的锚点贴合在一起.  ----- 其他引擎默认不是这样的,所以再跨平台导数据的时候,要注意这些细微的差别 ...

  6. 二维图形的矩阵变换(三)——在WPF中的应用矩阵变换

    原文:二维图形的矩阵变换(三)--在WPF中的应用矩阵变换 UIElement和RenderTransform 首先,我们来看看什么样的对象可以进行变换.在WPF中,用于呈现给用户的对象的基类为Vis ...

  7. EXP-00091 Exporting questionable statistics

    今天在我们对Oracle做EXP的过程中,出现EXP-00091 Exporting questionable statistics.的信息,但是也提示导出成功.最好查询了下发现其实它就是exp的er ...

  8. HDU4857——逃生(反向建图+拓扑排序)(BestCoder Round #1)

    逃生 Description 糟糕的事情发生啦,现在大家都忙着逃命.但是逃命的通道很窄,大家只能排成一行. 现在有n个人,从1标号到n.同时有一些奇怪的约束条件,每个都形如:a必须在b之前.同时,社会 ...

  9. P147、面试题26:复杂链表的复制

    题目:请实现ComplexListNode* Clone(ComplexListNode* pHead),复制一个复杂链表.在复杂链表中,每个结点除了有一个m_pNext指针指向下一个结点外,还有一个 ...

  10. PHP session过期时间

    如何设置一个严格30分钟过期的Session 今天在我的微博(Laruence)上发出一个问题: 我在面试的时候, 经常会问一个问题: “如何设置一个30分钟过期的Session?”, 大家不要觉得看 ...