• 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. Extjs4.2——Panel

    一.Panel的border属性: 示例: Ext.create('Ext.panel.Panel', { title: 'Hello', width: 200, height:100, border ...

  2. 支付宝Unity

    原地址:http://blog.csdn.net/sgnyyy/article/details/20444627 说明:支付宝Android的SDK接入只有一个接口,付费. 1. Android代码的 ...

  3. 不定长内存池之apr_pool

    内存池可有效降低动态申请内存的次数,减少与内核态的交互,提升系统性能,减少内存碎片,增加内存空间使用率,避免内存泄漏的可能性,这么多的优点,没有理由不在系统中使用该技术. 内存池分类: 1.      ...

  4. 什么是 .manifest 文件

    恩,为了大家都能很方便的理解,我将尽量简单通俗地进行描述. [现象]对这个问题的研究是起源于这么一个现象:当你用VC++2005(或者其它.NET)写程序后,在自己的计算机上能毫无问题地运行,但是当把 ...

  5. (转)eclipse快捷键

    Eclipse常用快捷键 Eclipse的编辑功能非常强大,掌握了Eclipse快捷键功能,能够大大提高开发效率.Eclipse中有如下一些和编辑相关的快捷键. 1. [ALT+/] 此快捷键为用户编 ...

  6. linux MySQL安装配置

    执行下面的命令初始化授权表: ./scripts/mysql_install_db --user=mysql

  7. win8 hyper-v 禁用不必卸载虚拟机

    转载:http://tylzwp.blogbus.com/logs/232938121.html 禁用hyperv的目的是使用之前在用的VMware的虚拟机,不必重新处理一遍. 具体操作: 1确报之前 ...

  8. node.js 异步式I/O 与事件驱动

    Node.js 最大的特点就是异步式 I/O(或者非阻塞 I/O)与事件紧密结合的编程模式.这种模式与传统的同步式 I/O 线性的编程思路有很大的不同,因为控制流很大程度上要靠事件和回调函数来组织,一 ...

  9. js setTimeout深度递归后完成回调

    setTimout原型: iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])     setTimeout有两种形式 se ...

  10. The document has been modified outside of Code Composer. Would you like to reload the file

    2013-06-20 10:03:32 烧写过程是合众达给出的文档 problem: I'm new to using Code Composer Studio 3.3 and am having a ...