《代码整洁之道》阅读笔记
1.注釋篇
Any comment that forces you to look in another module for the meaning of that comment has failed to communicate
to you and is not worth the bits it consumes.?
任何迫使讀者查看其他模塊的注釋,都沒能與讀者溝通好,不值所費。
If authors aren't paying attention when comments are written(or pasted), why should readers be expected to profit from them?
如果作者在寫注釋時都沒花心思,怎么能指望讀者從中獲益呢?
2.格式篇
Unfortunately, most tools for reformatting code are blind to the precedence of operators and impose the same spacing throughout. So subtle spacings like those shown above tend to get lost after you reformat the code.?
不幸的是,多數(shù)代碼格式化工具都會漠視運算符優(yōu)先級,從頭到尾采用同樣的空格方式。在重新格式化代碼后,以上這些微妙的空格用法就消失殆盡了。
注:微妙的空格用法指高優(yōu)先級運算符和操作數(shù)之間不用空格,而低優(yōu)先級運算符則可以添加空格。
3.函數(shù)與數(shù)據(jù)結(jié)構篇
The Law of Demeter:
- Each unit should have only limited knowledge about other units: only units "closely" related to the current unit.
- Each unit should only talk to its friends; don't talk to strangers.
- Only talk to your immediate friends.
相關闡述:http://www.importnew.com/10501.html
4.錯誤處理篇
We can write robust clean code if we see error handling as a separate concern, something that is viewable independently of our main logic.?
如果將錯誤處理隔離看待,隔離于主要邏輯之外,就能寫出強固而整潔的代碼。
5.單元測試篇
Test code is just as important as production code. It is not a second-class citizen. It requires thought, design, and care. It must be kept as clean as production code.?
測試代碼和生產(chǎn)代碼一樣重要。它可不是二等公民。它需要被思考、被設計和被照料。它必須像生產(chǎn)代碼一般保持整潔。
6.類篇
The Single Responsibility Principle (SRP)2 states that a class or module should have one, and only one, reason to change.?
Classes should have one responsibility—one reason to change.?
單一權責原則認為,類或模塊應有且只有一條加以修改的理由。
類只應有一個權責——只有一條修改的理由。
7.迭進
Once we have tests, we are empowered to keep our code and classes clean. We do this by incrementally refactoring the code. For each few lines of code we add, we
pause and reflect on the new design. Did we just degrade it? If so, we clean it up and run our tests to demon- strate that we haven’t broken anything. The fact that
we have these tests eliminates the fear that cleaning up the code will break it!?
有了測試,就能保持代碼和類的整潔,方法就是遞增式地重構代碼。對于我們添加的每幾行代碼,我們就要停頓下來并思考新的設計。設計退步了嗎?如果是,就要清理它,并且運行測試以
保證沒有破壞任何東西。測試消除了對清理代碼就會破壞代碼的恐懼。
8.并發(fā)編程篇
The synchronized keyword introduces a lock. All sections of code guarded by the same lock are guaranteed to have only one thread executing through them at any
given time. Locks are expensive because they create delays and add overhead. So we don’t want to litter our code with synchronized statements. On the other hand,
critical sec- tions13 must be guarded. So we want to design our code with as few critical sections as possible.?
java關鍵字synchronized制造了鎖。同一個鎖維護的所有代碼區(qū)域在任一時刻保證只有一個線程執(zhí)行。瑣是昂貴的,因為它們帶來了延遲和額外開銷。所以我們不愿意將代碼扔給synchronized
語句了事。另一方面,臨界區(qū)應該被保護起來。所以,應該盡可能少地設計臨界區(qū)。
This just reinforced the fact that different operating systems have different threading policies, each of which impacts the code’s execution. Multithreaded code behaves
differently in different environments.16 You should run your tests in every potential deployment environment.?
這正強調(diào)了不同操作系統(tǒng)有著不同線程策略的事實,不太的線程策略影響了代碼的執(zhí)行。在不同環(huán)境中,多線程代碼的行為也不一樣。應該在所有可能部署的環(huán)境中運行測試。
9.逐步改進篇-successive refinement
10.味道與啟發(fā)篇
A more subtle form is the switch/case or if/else chain that appears again and again in various modules, always testing for the same set of conditions. These should be replaced with polymorphism.
Still more subtle are the modules that have similar algorithms, but that don’t share similar lines of code. This is still duplication and should be addressed by using the TEM- PLATE METHOD,4 or STRATEGY5 pattern.?
(關于重復)較隱蔽的形式是在不同模塊中不斷重復出現(xiàn)、檢測同一組條件的switch/case或if/else鏈。可以用多態(tài)來代替之。
更隱蔽的形態(tài)是采用類似算法但具體代碼行不同的模塊。這也是一種重復,可以使用模板方法模式或策略模式來修正。
?
轉(zhuǎn)載于:https://www.cnblogs.com/sophia-yun/p/4471614.html
總結(jié)
以上是生活随笔為你收集整理的《代码整洁之道》阅读笔记的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转载】linux2.6内核initrd
- 下一篇: nib、xib、storyboard(故