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
前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发 ...
随机推荐
- javascript操作html元素CSS属性
下面先记录一下JS控制CSS所使用的方法. 1.使用javascript更改某个css class的属性... <style type="text/css"> .ori ...
- 解决Twitter Bootstrap Tab URL链接问题
例如这样的一个Tabs 代码: <ul class="nav nav-tabs" id="myTab"> <li class="ac ...
- SQL中约束和触发器的停用与启用
如何对SQL中的约束和触发器进行停用与启用,如果有外键约束则相关联表都要进行相应操作. ALTER TABLE TableName CHECK CONSTRAIT ALL --检查约束 ALTER T ...
- ORA-12012 error on auto execute of job 8887
*** ACTION NAME:(AUTO_SPACE_ADVISOR_JOB) -- ::58.046 *** MODULE NAME:(DBMS_SCHEDULER) -- ::58.046 ** ...
- django 的请求处理部分----WSGIHandler 源码分析 django1.5.5
从这里拉开django框架的帷幕..撕开,哈哈 主要涉及的类:django.core.handlers.base.BaseHandler,django.core.handlers.wsgi.WSGIH ...
- Python设计模式——观察者模式
需求:员工上班在偷偷看股票,拜托前台一旦老板进来,就通知他们,让他们停止看股票. 这里有两类人,一类是观察者,即员工,一类是通知者,即前台,员工在观察前台的状态,前台负责通知员工最新的动态. #enc ...
- 查看 usb info
mount -t usbfs /proc/bus/usb /proc/bus/usb cat /proc/bus/usb/devices
- <六> jQuery 获得内容和属性
获得内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - ...
- SQL Server 修改排序规则
Net stop mssqlserver Setup /QUIET /ACTION=REBUILDDATABASE /instancename=mssqlserver /SQLSYSADMINACCO ...
- display:inline-block元素间空白间隙问题
display:inline-block元素间有空白间隙,可以在父元素上加font-size:0