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

前方几天开始看Uncle Bob的《Clean
Code》,在率先章节里,作者讨论了这样的一个题材:什么样的code才是Clean
code。对于此问题,一千单Programmer可能会有一千单答案,所以作者请教了6独名牌的学者,问问他们对于这题目的眼光,以下三只凡是自身认为不那么虚之,比较起操作参考意义之对答:

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.

此处的几乎独根本是:

  • Elegant:意味着你的代码看起颇舒畅
  • Efficient:这个也许和Bjarne
    Stroustrup的世界有关,毕竟使用C++的地方大多是针对效率要求大高之园地。
  • Error handling complete:完备的错误处理机制,这表示关注细节。
  • 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.

这里的几个根本是:

  • Can be read and enhanced by others
  • 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,相对来说C++还是于实际的。

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. DRY: Duplication is the root of all evil in software
    design.

世家认为,什么样的代码才是好代码呢?