第一感觉:很有趣又非常实用的一本书,“Uncle Bob”的文笔相当幽默生动。代码质量与其整洁度成正比,大家都知道,但是如何打磨这些代码让它们变得整洁,这就是Bob大叔这本书所要传授的,在看本书的同时回忆一下自己是平时是如何编程的,会使阅读过程变得更有趣。
这本书前三章谈到了大量的小技巧和规则,例如使用可搜索的名称,分解函数,修改名称,消除重复,函数的第一规则是要短小,第二条规则是还要更短小,函数应该只做一件事,功能集中,有一个好名字(可以使用长而具有描述性的名称),它所带的最理想的参数数量是零。还有,类名和对象名应该是名词或名词短语,而方法名应当是动词或动词短语,减少代码依赖关系,让代码易读,等等。 其实这些小技巧和规则大家都知道,但是如何坚持使用和贯彻,这才是难点。
Chapter 4 Comment(注释),我很喜欢本章的引言“Don’t comment bad code—rewrite it.”—Brian W. Kernighan and P. J. Plaugher。还有一句话也说印象深刻“Keep in mind, however, that the only truly good comment is the comment you found a way not to write.”还有关于Position Markers位置标记的讨论,我个人就常常滥用Position Markers,书中谈到“ So use them very sparingly, and only when the benefit is significant. If you overuse banners, they’ll fall into the background noise and be ignored.” 这章还提到”Few practices are as odious as commenting-out code. Don’t do this!”,看完非常惭愧,我常常注释代码。
Chapter 5 Formatting(格式),这里有关于Vertical,Horizontal Formatting等等的讨论,这里摘抄一段话"It appears to be possible to build significant systems(FitNesse is close to 50,000 lines) out of files that are typically 200 lines long, with an upper limit of 500. Although this should not be a hard and fast rule, it should be consideredvery desirable. Small files are usually easier to understand than large files are."
Chapter 6 Objects and Data Structures (对象和数据结构)个人很喜欢这章,谈到了Data/Object Anti-Symmetry----数据、对象的反对称性,还有一些法则例如Data Transfer Objects(数据传送对象)。
chapter 7 Error Handling (错误处理),谈到了使用异常而非返回码还有给出异常发生的环境说明等等。
chapter 8 Boundaries(边界),讨论了使用第三方代码,整洁的边界等等问题。
chapter 9 Unit Tests(单元测试),个人非常喜欢这章,非常实用,这里讨论了Test-Driven Development,The Three Laws of TDD,这里摘书中几句“What makes a clean test? Three things. Readability, readability, and readability. Readability is perhaps even more important in unit tests than it is in production code.” “Tests are as important to the health of a project as the production code is. Perhaps they are even more important, because tests preserve and enhance the flexibility, maintainability, and reusability of the production code. So keep your tests constantly clean.”
Chapter 10 Classes,谈到了类的组织和内聚等等问题,有一句话令人印象深刻,The first rule of classes is that they should be small, the second rule of classes is that they should be smaller than that.哈够深刻吧。
chapter 11 Systems,引言中的话非常精妙,““Complexity kills. It sucks the life out of developers,it makes products difficult to plan, build, and test.”—Ray Ozzie, CTO, Microsoft Corporation”。 其实无论是设计系统或单独的模块,永远要忘记使用可以用的“最简单的东西”。
Chapter 12 Emergence(迭进),谈到了通过迭进设计,以达到整洁目的等等。
Chapter 13 Concurrency(并发编程),我觉得这章写的非常好,简单生动,如果对Concurrency programming有兴趣,推荐可以直接阅读这章。
chapter 14 Successive Refinement (关于逐步改进),从这章里摘抄一句话“So the solution is to continuously keep your code as clean and simple as it can be. Never let the rot get started.”
Chapter 15 JUnit Internals(JUnit框架的内幕)和Chapter 16 Refactoring SerialDate,关于(重构SerialDate),跳过,没有阅读。 Chapter 17 Smells and Heuristics,翻阅,没有仔细阅读。
个人一点小感想,如果本书能再出一个“精华简化版”(例如去掉一些例子和说明,去掉第15章,减少第17章 Smells and Heuristics(味道与启发)的篇幅,并把附录的大部分砍掉),缩减到大概200左右就完美了,200页的小册子可以放在身边,作为程序员编程时的童子军规则随时查阅 :D。刚刚发现这本书已经有了中文版,读中文版一定更愉快体会“Uncle Bob”想要告诉我们的简单而又重要的大道理“Writing clean code is what you must do in order to call yourself a professional. There is no reasonable excuse for doing anything less than your best.”。
1 条评论:
Hi,我最近也一直在看Robert Martin的Clean Code这本书,通过google到你的blog,又看了你blog的其他文章,觉得很有意思。
希望能交个朋友,有空可以向你讨教经验。
发表评论