• The Purpose of Formatting

    Code formatting is about communication, and communication is the professional developer's first order of business.

  • Vertical Formatting

    File size: less than 200~500 lines.

    • The Newspaper Metaphor

      We would like a source file to be like a newspaper article. The name should be simple but explanatory. The topmost parts of the source file should provide the high-level concepts and algorithms. Details should increase as we move downward, until at the end we find the lowest level functions and details in the source file.

    • Vertical Openness Between Concepts

      Each line represents an expression or a clause, and each group of lines represents a complete thought. Those thoughts should be separated from each other with blank lines.

      Each blank line is a visual cue that identifies a new and separate concept.

    • Vertical Density

      Lines of code that are tightly related should appear vertically dense.

    • Vertical Distance

      Concepts that are closely related should be kept vertically close to each other.

      • Variable Declarations

        Variables should be declared as close to their usage as possible.

      • Instance variables

        Should be declared at the top of the class.

      • Dependent Functions

        If one function calls another, they should be vertically close, and the caller should be above the callee, if at all possible.

      • Conceptual Affinity

        • a direct dependence
        • a similar operation
  • Horizontal Formatting

    Line width: less than 100~120.

    • Horizontal Openness and Density

      Use horizontal white space to disassociate things that are weakly related.

      e.g.

      public class Quadratic {
      public static double root1(double a, double b, double c) {
      double determinant = determinant(a, b, c);
      return (-b + Math.sqrt(determinant)) / (2*a);
      } public static double root2(double a, double b, double c) {
      double determinant = determinant(a, b, c);
      return (-b - Math.sqrt(determinant)) / (2*a);
      } private static double determinant(double a, double b, double c) {
      return b*b - 4*a*c;
      }
      }

      这里的行间空白主要体现在:

      • 赋值号 = 左右空格以突出赋值操作
      • 函数各参数之间空格分隔
      • 函数名与其后面的左括号不需要空格
      • 各种运算符左右空格以突出对应的操作

        上面代码有个特例就是乘号左右没有空白分隔。按照作者的解释,“它们属于较高优先级(high precedence)”。 这么写公式的逻辑看起来确实更清晰一些。可惜 Visual Studio 的格式化代码没有这么智能。

    • Horizontal Alignment

      No need. (used in assembly files)

    • Indentation

      To make the hierarchy of scopes visible.

    • Dummy Scopes

      Try to avoid them.

      Make sure that the dummy body is properly indented and surrounded by braces.

      Bad code:

      while (dis.read(buf, 0, readBufferSize) != -1)
      ;

      Good code:

      while (dis.read(buf, 0, readBufferSize) != -1)
      {
      ;
      }
  • Team Rules

    Every programmer has his own favorite formatting rules, but if he works in a team, then the team rules.

Clean Code – Chapter 5 Formatting的更多相关文章

  1. Clean Code–Chapter 7 Error Handling

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

  2. Clean Code – Chapter 4: Comments

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

  3. Clean Code – Chapter 3: Functions

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

  4. Clean Code – Chapter 2: Meaningful Names

    Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...

  5. Clean Code – Chapter 6 Objects and Data Structures

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

  6. “Clean Code” 读书笔记序

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

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

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

  8. Writing Clean Code 读后感

    最近花了一些时间看了这本书,书名是 <Writing Clean Code ── Microsoft Techniques for Developing Bug-free C Programs& ...

  9. 说说怎么写clean code

    前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发 ...

随机推荐

  1. [CSS]visibility 属性

    定义和用法 visibility 属性规定元素是否可见. 提示:即使不可见的元素也会占据页面上的空间.请使用 "display" 属性来创建不占据页面空间的不可见元素. 说明 这个 ...

  2. Open CASCADE 基础类(Foundation Classes)

    1 介绍(Introduction) 1 如何使用Open CASCADE技术(OCCT)基础类. This manual explains how to use Open CASCADE Techn ...

  3. 宅男福利--利用Python简单爬图

    Ver beta..代码粗陋. 使用说明以Windows为例, Python版本为2.7.6 确认你电脑已经安装了Python, Windows默认安装路径为C:\Python27.如果没有安装,先下 ...

  4. Django视图函数

    一.视图函数 1. 视图函数的第一个参数一定是一个HTTPRequest类型的对象,这个对象是Django自动创建的,具体形参名通常用request.通过这个对象,可以调用请求的一些参数,比如requ ...

  5. OFBiz进阶之环境搭建(eclipse)

    一. 环境准备 1. jdk1.6 下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive- ...

  6. Sqlmap下载安装与基础命令使用

    本文介绍一下Sqlmap的安装跟配置环境变量. 顺便附上一些常用的命令 SQLMAP-64位.Python 下载链接:http://pan.baidu.com/s/1c0D82fm 密码:d7ec P ...

  7. 野指针、NULL指针和void*

    一.野指针 “野指针”不是NULL指针,是指向“垃圾”内存的指针. “野指针”的成因主要有三种: (1)指针变量没有被初始化.任何指针变量刚被创建时不会自动成为NULL指针,它的缺省值是随机的,它会乱 ...

  8. django中外键关联表的查询随笔

    django中,如果一个数据库中的表之间有外键的话可以方便的通过一个表查询到其相关表的数据.如有下面三个model:class Blog(models.Model):    name = models ...

  9. location.hash && location.href

    hash:设置或获取 href 属性中在井号“#”后面的分段. href:设置或获取整个URL为字符串. 通过下面的测试你会发现区别,将代码放到你的HTML中,然后用浏览器打开,测试步骤: 点击“超链 ...

  10. linux登录后出现_bash-4.1#终端提示符异常

    如果使用root用户登录出现上述提示,则需要需要重建 /root .bash_profile文件: 1. vi /root .bash_profile 2. 输入如下内容 # .bashrc # Us ...