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
前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发 ...
随机推荐
- MAC自带的lnmp
MAC自身带有apache,php. 1.启动apache服务 sudo apachectl start/restart/stop 2.查看php的版本 php -v 3.让apache支持ph ...
- Python多线程启动http.server
OS: Windows 8.1 with update 关键字:Python3.4, http.server, Thread 例子代码如下: import os from threading impo ...
- Discuz CDN优化方案
DZ整体来说CDN是有点蛋疼的,因为毕竟琐碎,貌似大部分帖子都没有说全,这里罗列一下,给在用的孩儿们一点参考: 1.在后台设置CSS/JS走CDN路径,具体[全局]-[性能优化]-[服务器优化] 2. ...
- nutch 采集效率--设置采集间隔
fetcher.max.crawl.delay 默认是30秒,这里改为 5秒修改nutch-default.xml<property> <name>fetcher.max.cr ...
- <算法竞赛入门经典> 第8章 贪心+递归+分治总结
虽然都是算法基础,不过做了之后还是感觉有长进的,前期基础不打好后面学得很艰难的,现在才慢慢明白这个道理. 闲话少说,上VOJ上的专题训练吧:http://acm.hust.edu.cn/vjudge/ ...
- C#博文搜集
1. abstract (抽象类) 参考1 2. interface (接口) 参考1 3. 委托 C#委托学习
- ANDROID_MARS学习笔记_S01原始版_008_Looper\Bundle异步消息处理
一.流程 1.自定义Handler,重写handleMessage(Message msg),用msg得到bundle,从而得到传递过来的数据 2.开启android.os.HandlerThread ...
- python还不能作为主要编程语言的原因:
1.不太熟悉,容易犯新手错误,2.调试方法不同3.写了一个函数,是否语法正确,不能知道,只有具体调用它的时候才知道4.编辑器太业余,没有输入联想功能5.要查找一个函数或变量在哪里定义的,只能通过搜索的 ...
- Android开发之ListView-BaseAdapter的使用
ListView优化原则: UI优化: listview条目与条目之间的间隙的分割内容 : android:divider="@android :color/transparent" ...
- mysql show variables系统变量详解
mysql系统变量详解 mysqld服务器维护两种变量.全局变量影响服务器的全局操作.会话变量影响具体客户端连接相关操作. 服务器启动时,将所有全局变量初始化为默认值.可以在选项文件或命令行中指定的选 ...