Clean Code – Chapter 5 Formatting
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的更多相关文章
- Clean Code–Chapter 7 Error Handling
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return ...
- Clean Code – Chapter 4: Comments
“Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments ...
- Clean Code – Chapter 3: Functions
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and s ...
- 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 ...
- Clean Code – Chapter 6 Objects and Data Structures
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abst ...
- “Clean Code” 读书笔记序
最近开始研读 Robert C.Martin 的 “Clean Code”,为了巩固学习,会把每一章的笔记整理到博客中.而这篇博文作为一个索引和总结,会陆续加入各章的笔记链接,以及全部读完后的心得体会 ...
- 代码整洁之道Clean Code笔记
@ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Function ...
- Writing Clean Code 读后感
最近花了一些时间看了这本书,书名是 <Writing Clean Code ── Microsoft Techniques for Developing Bug-free C Programs& ...
- 说说怎么写clean code
前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发 ...
随机推荐
- 将ecshop中的session机制重写,从DB移植到Memcache中去
<?php if (!defined('IN_ECS')) { die('Hacking attempt'); } /*------------------------------------- ...
- Centos6.5下rsync文件同步安装配置及遇到的问题解决方法
实验节点如下: 源节点:192.168.0.111 备份节点:192.168.0.112 ------------------------------以下部分在两个节点上执行1.防火墙开放873端口( ...
- Python原型模式
如果想根据现有对象复制出新的对象并对其修改,可以考虑原型模式(Prototype Pattern) class Point: __slots__ = ("x", "y&q ...
- __sync_fetch_and_add
最近在公司离职的前辈写的代码哪里看到了__sync_fetch_and_add这个东东.比较好奇.找些资料学习学习 http://www.lxway.com/4091061956.htm http:/ ...
- 2014年辛星完全解读Javascript第二节
本小节我们讲解一下Javascript的语法,虽然js语言非常简单,它的语法也相对好学一些,但是不学总之还是不会的,因此,我们来一探究竟把. ********注释************* 1.我们通 ...
- Java线程(转)
1.Java线程的实现. 第一种方式可以通过继承Thread.在Thread类中,有许多关于线程操作的方法,比如:sleep();activeCount();等 继承Thread的代码: public ...
- POJ 3254 压缩状态DP
题意:一个矩形网格,可以填0或1, 但有些位置什么数都不能填,要求相邻两个不同时为1,有多少种填法.矩形大小最大 12*12. 压缩状态DP大多有一个可行的state的范围,先求出这个state范围, ...
- Boost IPC Persistence Of Interprocess Mechanisms 例子
下面这一段摘抄自 Boost 1_55_0 的文档,显然标注了 每一个的生命期. One of the biggest issues with interprocess communication m ...
- Spring MVC 注解和XML的区别
注解与XML配置的区别 注解:是一种分散式的元数据,与源代码紧绑定. xml:是一种集中式的元数据,与源代码无绑定. 因此注解和XML的选择上可以从两个角度来看:分散还是集中,源代码绑定/无绑定. ...
- java修改远程服务器密码
积累: 1. echo 用户名:新密码 | chpasswd