• 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. json在线校验

    弄了一个在线校验,清爽无广告,欢迎大家收藏   http://www.zhhoney.com/

  2. FolderBrowserDialog组件选择文件夹

    1.选择路径 this.folderBrowserDialog1.ShowDialog(); if (this.folderBrowserDialog1.ShowDialog() == DialogR ...

  3. MVC-ActionResult解说

    HttpNotFoundResult: 专门用来响应Http404找不到网页的错误,在System.Web.Mvc.Controller类别中内建了一个HttpNotFound()方法,可以很方便的回 ...

  4. Codeforces Round #327 div2

    Problem_A(591A): 题意: 有一段长度为l的路,两个人分别在两个端点,1, l. 现在已知每个人的速度为p,q. 求第一个人(初始位置在1)在他们第二次相遇的时候的位置. 当他们相遇的时 ...

  5. Delphi XE5 android 图解为Android应用制作签名

    本文摘自 http://redboy136.blog.163.com/blog/static/107188432201381872820132 要发布android应用,必须做签名的.看一下具体的操作 ...

  6. JSON漫谈

    JSON: JavaScript Object Notation(JavaScript 对象表示法),JSON 是存储和交换文本信息的语法.类似 XML.JSON 比 XML 更小.更快,更易解析. ...

  7. RWD

    http://webdesignerwall.com/tutorials/responsive-design-with-css3-media-queries http://www.webdesigns ...

  8. linux awk命令详解(转)

    http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html 简介 awk是一个强大的文本分析工具,相对于grep的查找,sed的编 ...

  9. nginx Location配置总结(转)

    本文部分转自:http://cssor.com/nginx-location-configuration.html 一. 开头 语法规则: location [=|~|~*|^~] /uri/ { … ...

  10. Integer.valueOf与Integer.parseInt的小疑惑

    参考博客: http://www.importnew.com/9162.html 测试代码如下: public class Main { /** * 备注:结果跟你的JDK版本有关系: * * 我的是 ...