《Clean Code》阅读:在大师们的眼中,什么样的代码才是好代码

前几天开始看Uncle Bob的《Clean Code》,在第一章里,做者讨论了这样的一个问题:什么样的code才是Clean code。对于这个问题,一千个Programmer可能会有一千个答案,因此做者请教了6个著名的专家,问问他们对于这个问题的见解,如下三个是我认为不那么虚的,比较有操做参考意义的回答:git

1. Bjarne Stroustrup, C++之父

I like my code to be elegant and efficient. The logic should be straightforward to make it hard for bugs to hide, the dependencies minimal to ease maintenance, error handling complete according to an articulated strategy, and per- formance close to optimal so as not to tempt people to make the code messy with unprinci- pled optimizations. Clean code does one thing well.程序员

这里的几个重点是:github

  • Elegant:意味着你的代码看起来很舒服express

  • Efficient:这个可能跟Bjarne Stroustrup的领域有关,毕竟使用C++的地方大可能是对效率要求很高的领域。app

  • Error handling complete:完备的错误处理机制,这意味着关注细节。ide

  • Does one thing well:就是咱们所说的,Single Responsibility!测试

2. Dave Thomas, 出版公司The Pragmatic Bookshelf Co-founder,《Pragmatic Programmer》(中文叫程序员修炼之道)、《Programming Ruby》做者。

Clean code can be read, and enhanced by a developer other than its original author. It has unit and acceptance tests. It has meaningful names. It provides one way rather than many ways for doing one thing. It has minimal dependencies, which are explicitly defined, and pro- vides a clear and minimal API. Code should be literate since depending on the language, not all necessary information can be expressed clearly in code alone.idea

这里的几个重点是:设计

  • Can be read and enhanced by otherscode

  • Unit and acceptance tests

  • Meaningful names

  • Minimal dependency, minimal API

这里的test和Minimal API是咱们应该注意的东西,

3. Ron Jeffries 《Extreme Programming Installed》 《Extreme Programming Adventures in C#》的做者

In recent years I begin, and nearly end, with Beck’s rules of simple code. In priority order, simple code:
• Runs all the tests;
• Contains no duplication;
• Expresses all the design ideas that are in the system;
• Minimizes the number of entities such as classes, methods, functions, and the like.
... Of these, I focus mostly on duplication. When the same thing is done over and over, it’s a sign that there is an idea in our mind that is not well represented in the code. I try to figure out what it is. Then I try to express that idea more clearly.
Expressiveness to me includes meaningful names, and I am likely to change the names of things several times before I settle in.... I also look at whether an object or method is doing more than one thing. If it’s an object, it probably needs to be broken into two or more objects. If it’s a method, I will always use the Extract Method refactoring on it, resulting in one method that says more clearly what it does, and some submethods saying how it is done...

在这里,Ron Jeffries甚至把test放在第一位。
至于做者的最后一条:Minimizes the number of entities such as classes, methods, functions, and the like。我认为做者应该是有前提的。否则的话,若是以那个为标准,那么全部的代码都放到一个类,一个方法里面好了,这样明显是不行的。并且做者后面本身也提到,会把一个大的类分红两个小的,会用extract method来分解一个很长的方法。整体来讲,应该大多数人都会认同:几个小的类比一个大的类要好,几个小的方法比一个大的方法要好。

下面3个回答相对来讲就比较虚了,你们看看就好。

4. Grady Booch, 《Object Oriented Analysis and Design with Applications》做者

Clean code is simple and direct. Clean code reads like well-written prose. Clean code never obscures the designer’s intent but rather is full of crisp abstractions and straightforward lines of control.

简单直接,如诗通常, straightforward lines of control,说了等于没说。abstraction,相对来讲仍是比较实在的。

5. Michael Feathers, 《Working Effectively with Legacy Code》做者

I could list all of the qualities that I notice in clean code, but there is one overarching quality that leads to all of them. Clean code always looks like it was written by someone who cares. There is nothing obvious that you can do to make it better. All of those things were thought about by the code’s author, and if you try to imagine improvements, you’re led back to where you are, sitting in appreciation of the code someone left for you—code left by some- one who cares deeply about the craft.

这里的重点是Care。可是什么样的代码是被author care过得呢?这里没有明确的指标能够依据,只能靠咱们本身去判断。

6. Ward Cunningham, Wiki发明人,Fit发明人,eXtreme Programming共同发明人. Design Patterns、OO思想领袖。The godfather of all those who care about code.

You know you are working on clean code when each routine you read turns out to be pretty much what you expected. You can call it beautiful code when the code also makes it look like the language was made for the problem.

“Pretty much what you expected”,可是每一个人expect的代码的样子都是不同的,一个初学者expected的代码的样子可能相对也会比较初级,难道那个也能称做clean code吗?I might have a different opinion.

总结一下,我我的认为如下几点,很是值得咱们参考:

  1. Single Responsibility. 一个类只作它该作的事情,一个方法作一件事。

  2. Minimal API:一个类对外暴露的接口应该是尽可能少的,其实这也就意味着多用Composition,少用继承,由于继承,意味着这个类继承了父类全部的接口,这跟Minimal API明显是相对的。

  3. Unit Test:这不是一件可有可无的事情,不要说“仅仅是为了测试”这样的话。。。

  4. Naming:这也是一件很是重要的事情,类名,方法名,变量名等等,这都是很是影响可读性的。哪怕写起来不大顺手,也须要为了更好的可读性,设计一个更好的接口。为何?由于读代码的时间比写代码的时间多出10倍不止,这个在书中也有提到,是经过人们对Emacs的使用状况,真实统计出来的。从另一个角度来说,代码写出来是一次性的,而以后被阅读的次数倒是没法估计的。

  5. No duplication: Duplication is the root of all evil in software design.

你们以为,什么样的代码才是好代码呢?

有任何意见或建议,或者发现文中任何问题,欢迎留言!

更多文章请访问我的博客
做者:邹小创
Github:https://github.com/ChrisZou邮件:happystriving@126.com

相关文章
相关标签/搜索