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
前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发 ...
随机推荐
- Oracle OEM重建
参考博客:http://blog.csdn.net/tianlesoftware/article/details/4702978 测试环境: C:\Users\Administrator>sql ...
- wpf单实例运行
默认情况下我们可以打开一个应用程序多个实例,例如你双击一个exe多次.当然有些时候这么做会带来很多好处,但是有时我们又不希望这么做,要避免这个问题其实很简单,同WinForm中单实例运行一个应用是一样 ...
- 开发设计模式(九)门面模式(Facade Pattern)
什么是门面模式? 门面模式要求一个子系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行.门面模式提供一个高层次的接口,使得子系统更易于使用. 大家都写过纸质的信件吧,比如给女朋友写 ...
- 一步步学习NHibernate(3)——NHibernate增删改查
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们配置了以下NHibernate的运行环境, 并介绍了NHibernate的中两个非常中重要的接口"I ...
- 基于Python的TCP阻塞式echo服务器
上述问题的出现是因为没有设置listen函数 from socket import * from time import ctime HOST = '' PORT = 21567 BUFSIZ = 1 ...
- python 读取SQLServer数据插入到MongoDB数据库中
# -*- coding: utf-8 -*-import pyodbcimport osimport csvimport pymongofrom pymongo import ASCENDING, ...
- JAVA File常用的API介绍
package coreJava; import java.io.File; import java.io.IOException; public class FileDemo { public st ...
- MySQL注入中load_file()函数的应用
常用的一些Load_File()函数攻击手法:
- top 10 js mvc
http://codebrief.com/2012/01/the-top-10-javascript-mvc-frameworks-reviewed/ http://www.iteye.com/new ...
- 用CURL来实现file_get_contents函数:curl_file_get_contents
<?php $url='http://www.bamuyu.com/news/zixun/list_7_2.html'; $content=curl_file_get_contents($url ...