• 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. PHP 生成随机浮点数

    <?php /** * @desc 获取随机浮点数(支持最大最小值参数互换) * @date 2014-11-07 17:04:21 * * @param int|\最小浮点数 $min 最小浮 ...

  2. Hive优化(转)

    一.join优化 Join查找操作的基本原则:应该将条目少的表/子查询放在 Join 操作符的左边.原因是在 Join 操作的 Reduce 阶段,位于 Join 操作符左边的表的内容会被加载进内存, ...

  3. win2003 sp2+iis 6.0上部署.net 2.0和.net 4.0网站的方法

    网站环境 IIS6.0,操作系统Windows server2003 sp2,服务器之前已经部署了.net 2.0和asp的网站,现在要部署新开发的.net 4.0网站.本来认为很简单,却遇到了很多问 ...

  4. 标准初始化css样式表

    body { font-size:12px; line-height:1.3; font-family:'微软雅黑', Tahoma,Helvetica,Arial,'宋体', sans-serif; ...

  5. jQuery name checked 模糊查找匹配ID

    ("div[name='jobTitle']") $("#aDiv").find("input[type='checkbox']:checked&qu ...

  6. c# 捕捉键盘按键

    //esc退出窗体  protected override bool ProcessCmdKey(ref System.Windows.Forms.Message msg , System.Windo ...

  7. 物理地址为20位 如10000H 用段地址*16+偏移地址表示

    段地质在cpu中,为16位 段地质*16则变成物理首地址20位,这个物理首地址必定是16的倍数. 偏移地址16位, 则表明一个段的大小为64k. 同时也表明16位地址的寻址能力为64kb

  8. thinkphp 模板替换

    具体详见tp手册. 如果需要修改模板替换映射路径. 则需: 'TMPL_PARSE_STRING'=>array( '__PUBLIC__'=>__ROOT__.'/'.APP_NAME. ...

  9. QML嵌入到QWidget中方法

    简介 嵌入方法有两种一种是直接拖控件,另一种是cpp代码动态生成, 控件方式 动态代码生成 QQuickWidget *m_quickWidget=new QQuickWidget(); QUrl s ...

  10. yii 验证器和验证码

    http://www.yiiframework.com/doc/api/1.1/CCaptcha http://www.cnblogs.com/analyzer/articles/1673015.ht ...