clean code 读书笔记一

什么是 clean code ?

大神对优雅代码的定义:

    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.
                     ——  Bjarne Stroustrup, inventor of C++
                         and author of The C++ Programming
                         Language

    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.

                 ——  Grady Booch, author of Object
                     Oriented Analysis and Design with
                     Applications

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 depen-
dencies, 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.
                             —— “Big” Dave Thomas, founder
                                 of OTI, godfather of the
                                 Eclipse strategy


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.

With modern coding tools such as Eclipse,renaming is quite inexpensive, so it doesn’t trouble me to change. Expressiveness goesbeyond names, however. 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.

In addition, the collection abstraction often calls my attention to what’s “really” going on, and keeps me from running down the path of implementing arbitrary collection behavior when all I really need is a few fairly simple ways of finding what I want.

Reduced duplication, high expressiveness, and early building of simple abstractions.

That’s what makes clean code for me.

—— Ron Jeffries, author of Extreme Programming

Installed and Extreme Programming

Adventures in C#(值得反复看看)

要做到

  • 直白易读
  • 意图明确,实事求是,拒绝让读者猜测
  • 分单元 ,要有验收测试?(有unitTest?我不懂,如何做到?求高手留言)
  • 精心关注细节,如 一个命名等等
  • 拒绝重复、高抽象度

    一个对象、方法、类 只做一件事情,否则抽象出来,单独实现。

    一个方法抽象出主要逻辑目的,然后实现,并在其中分为子逻辑去实现

好处是暴漏出bug,解耦等等

时间: 2024-08-26 15:13:32

clean code 读书笔记一的相关文章

Clean Code 读书笔记三

clean code 之方法(函数) - 短小 ,再短小,更短小 20行最佳 只做一件事 准确说来每个方法应该是只做抽象概念上的的一件事 只做一件事的方法是无法把逻辑分段的 自顶向下的代码 To say this differently, we want to be able to read the program as though it were a set of TO paragraphs, each of which is describing the current level of

Clean Code 读书笔记五

Use Exception Rather than Return Code(使用异常而不是错误处理) public class DeviceController {... public void sendShutDown() { DeviceHandle handle = getHandle(DEV1); // Check the state of the device if (handle != DeviceHandle.INVALID) { // Save the device status

Clean Code 读书笔记九

要点汇总: 一般性问题 方法名称应该准确表达其具体行为 比如: Date newDate = date.add(5)//加5天?5个月? 最好使用addDaysTo 或increaseByDays 明确方法的行为. 使用多态 代替 switch或if else 比如: class RequestHandler { public void handleRequest(int action) { switch(action) { case LOGIN: doLogin(); break; case

Clean code 读书笔记二

细节汇总: 拒绝在容器类型的命名中使用 该容器类型 List flaggedCells = new ArrayList(); As we'll see later on, even if the container is a List , it's probably better not to encode the container type into the name. -相同的意思,使用一致的拼写,并保证专词专意,一次一意 Spelling similar concepts similar

《Clean Code》读书笔记——第二周

本周我阅读了<Clean Code>. "神在细节中!",建筑家范德罗如是说.他当然专注于基于宏伟构架之上的永恒建筑形式,他也同样为自己设计的建筑挑选门把手.同样软件开发也是这样,小处见大.在宏伟的建筑作品中,我们也要关注细节的回响.重点便是整理,从而达成Clean.一个很好的例子是对于变量命名,认真对待每个变量名.书中作者说,我们就像一群代码猴子,无视混乱无序,失去代码的真谛.整洁的代码正是迈向编程之美的基础,重要性毋庸置疑. 作者断言,我们永远需要代码.我们可以创造各种

读书笔记:《梦断代码Dreaming in Code》

读书笔记:<梦断代码Dreaming in Code> 拿到<梦断代码>书后,一口气翻了一遍,然后又用了3天时间仔细读了一遍,也不禁掩卷长叹一声,做软件难.虽难,仍要继续走下去,可以把软件的范围限制得小一些,用敏捷方法等过程会使软件失败的风险小一些,毕竟我们还没有写书上所说的那样的大型软件. 第0章 软件时间 一开始看到第0章并没有感觉多么奇怪,可能程序员的思维方式已经固化在大脑中了,但读到作者关于第0章无意搞笑时,也不禁哑然失笑,为什么程序员要 从0开始计数?因为计算机从0开始计

OCP读书笔记(25) - 题库(ExamE)

401.Which of the following are correct about block media recovery? (Choose all that apply.)A. Physical and logical block corruption is recorded automatically in V$DATABASE_BLOCK_CORRUPTION.B. Logical corruptions are repairable by BMR.C. Physical corr

OCP读书笔记(26) - 题库(ExamF)

501.Note the output of the following query;SQL> SELECT flashback_archieve_name, status FROM dba_flashback_archieve;FLASHBACK_ARCHIEVE_NAME STATUSFLA1You executed the following command to enable Flashback Data Archive on the EXCHANGB_PATE table:ALTER

The Pragmatic Programmer 读书笔记之一 DRY-Don’t Repeat Youself

 The Pragmatic Programmer读书笔记之一 DRY-Don't Repeat Youself 虽然自己买了很多软件工程方面的书,但是因为时间的问题,一直没有静下心来充充电.最近因为感觉自己在编程方面迫切需要有进一步的提高,于是打算好好的研读一下Andrew Hunt和David Thomas合著的<The Pragmatic Programmer:From Journeyman toMaster>.本来我是比较偏好原版的,买的书不是原版就是影印版,但是由于该书原版价格有