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…
Error handling is important, but if it obscures logic, it's wrong. Use Exceptions Rather Than Return Codes Separate the normal operations with error handlings. e.g. Bad code: public class DeviceController { ... public void sendShutDown() { DeviceHand…
“Don’t comment bad code—rewrite it.”——Brian W.Kernighan and P.J.Plaugher The proper use of comments is to compensate for our failure to express ourself in code. Truth can only be found in one place: the code. Comments Do Not Make Up for Bad Code Rath…
Small Blocks and Indenting The blocks within if statements, else statements, while statements, and so on should be one line long. Probably that line should be a function call. Functions shouldn't be large enough to hold nested structures. The indent…
Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is used. e.g. Bad code: public List<int[]> getThem(){ List<int[]> list1 = new ArrayList<int[]>(); for (int[] x : theList) if (x[0] == 4) list1…
Data Abstraction Hiding implementation Data/Object Anti-Symmetry Objects hide their data behind abstractions and expose function that operate on that data. Data structure expose their data and hava no meaningful functions. Procedural code(code using…
最近开始研读 Robert C.Martin 的 “Clean Code”,为了巩固学习,会把每一章的笔记整理到博客中.而这篇博文作为一个索引和总结,会陆续加入各章的笔记链接,以及全部读完后的心得体会. 这里也引用一下第一章中提到的 LeBlanc's law: Later equals never. 看到这句话时,脑海中想到的中文翻译居然是 “有些事现在不做,一辈子都不会做了”.所以,为了不留遗憾,开始 "clean code" 吧. 以下为各章读书笔记目录链接: Chapter 2…
@ 目录 第 1 章 Clean Code 整洁代码(3星) ?为什么要整洁的代码 ?什么叫做整洁代码 第 2 章 Meaningful Names 有意义的命名(3星) 第 3 章 Functions 函数(3星) 第 4 章 Comments 注释(2星) 第 5 章 Formatting 格式 (1星) 第 6 章 Objects and Data Structures 对象和数据结构(4星) 第 7 章 Error Handling 错误处理(4星) 第 9 章 Unit Tests 单…
最近花了一些时间看了这本书,书名是 <Writing Clean Code ── Microsoft Techniques for Developing Bug-free C Programs> 这里主要总结了一些里面的编程思想. 为空语句加上NULL 当需要使用空语句的时候,最好写上NULL, 比如: if (music_on()) NULL; else turn_it_on(); 参数类型相同的问题 如果函数中两个参数的类型相同,如果用户调用这个函数时错误替换了参数的顺序,就会出现问题.…
前两天参加了公司组织的一个培训,主题是“如何写出好的代码” ,刚看到这个主题,第一反应是又不知道是哪个培训机构来忽悠钱的!老大安排了,就去听听呗. 说实在的,课程内容没有什么新鲜的东西,就是讲讲如何发现代码的坏味道,如何重构函数,如何修改遗留系统的代码.这些东西从本科到研究生到实习到正式工作,也不知道看过多少听过多少了,话说本科的课程设计也和代码重构相关,私底下重构和设计模式方面的书也没少看,感觉应该没啥好培训的,不过两天的课程听完,打心眼里觉得来对了,意犹未尽! 课程结束了,感觉需要把这两天课…